This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Reduce false positives for @hsh{$s} and @ary[$s] warnings
[perl5.git] / perly.y
CommitLineData
a0d0e21e 1/* perly.y
a687059c 2 *
f05e27e5 3 * Copyright (c) 1991-2002, 2003, 2004, 2005, 2006 Larry Wall
2eee27d7 4 * Copyright (c) 2007, 2008, 2009, 2010, 2011 by Larry Wall and others
a687059c 5 *
9ef589d8
LW
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
8d063cd8 8 *
a0d0e21e
LW
9 */
10
11/*
12 * 'I see,' laughed Strider. 'I look foul and feel fair. Is that it?
4ac71550 13 * All that is gold does not glitter, not all those who wander are lost.'
f05e27e5 14 *
4ac71550
TC
15 * [p.171 of _The Lord of the Rings_, I/x: "Strider"]
16 */
17
18/*
f05e27e5 19 * This file holds the grammar for the Perl language. If edited, you need
166f8a29
DM
20 * to run regen_perly.pl, which re-creates the files perly.h, perly.tab
21 * and perly.act which are derived from this.
22 *
f05e27e5
DM
23 * Note that these derived files are included and compiled twice; once
24 * from perly.c, and once from madly.c. The second time, a number of MAD
25 * macros are defined, which compile in extra code that allows the parse
26 * tree to be accurately dumped. In particular:
27 *
28 * MAD defined if compiling madly.c
29 * DO_MAD(A) expands to A under madly.c, to null otherwise
30 * IF_MAD(a,b) expands to A under madly.c, to B otherwise
31 * TOKEN_GETMAD() expands to token_getmad() under madly.c, to null otherwise
32 * TOKEN_FREE() similarly
33 * OP_GETMAD() similarly
34 * IVAL(i) expands to (i)->tk_lval.ival or (i)
35 * PVAL(p) expands to (p)->tk_lval.pval or (p)
36 *
166f8a29
DM
37 * The main job of of this grammar is to call the various newFOO()
38 * functions in op.c to build a syntax tree of OP structs.
61296642 39 * It relies on the lexer in toke.c to do the tokenizing.
29522234
DM
40 *
41 * Note: due to the way that the cleanup code works WRT to freeing ops on
42 * the parse stack, it is dangerous to assign to the $n variables within
43 * an action.
166f8a29
DM
44 */
45
0de566d7 46/* Make the parser re-entrant. */
8d063cd8 47
0de566d7 48%pure_parser
8d063cd8 49
f05e27e5
DM
50/* FIXME for MAD - is the new mintro on while and until important? */
51
28ac2b49 52%start grammar
9d116dd7 53
8d063cd8 54%union {
d5c6462e
DM
55 I32 ival; /* __DEFAULT__ (marker for regen_perly.pl;
56 must always be 1st union member) */
79072805
LW
57 char *pval;
58 OP *opval;
59 GV *gvval;
f05e27e5
DM
60#ifdef PERL_IN_MADLY_C
61 TOKEN* p_tkval;
123d08c9 62 TOKEN* i_tkval;
f05e27e5
DM
63#else
64 char *p_tkval;
123d08c9
DM
65 I32 i_tkval;
66#endif
67#ifdef PERL_MAD
68 TOKEN* tkval;
f05e27e5 69#endif
8d063cd8
LW
70}
71
78cdf107 72%token <ival> GRAMPROG GRAMEXPR GRAMBLOCK GRAMBARESTMT GRAMFULLSTMT GRAMSTMTSEQ
28ac2b49 73
7c70caa5 74%token <i_tkval> '{' '}' '[' ']' '-' '+' '$' '@' '%' '*' '&' ';' '=' '.'
f0fcb552 75
ea25a9b2 76%token <opval> WORD METHOD FUNCMETH THING PMFUNC PRIVATEREF QWLIST
7eb971ee 77%token <opval> FUNC0OP FUNC0SUB UNIOPSUB LSTOPSUB
88e1f1a2 78%token <opval> PLUGEXPR PLUGSTMT
f05e27e5 79%token <p_tkval> LABEL
123d08c9
DM
80%token <i_tkval> FORMAT SUB ANONSUB PACKAGE USE
81%token <i_tkval> WHILE UNTIL IF UNLESS ELSE ELSIF CONTINUE FOR
82%token <i_tkval> GIVEN WHEN DEFAULT
be25f609 83%token <i_tkval> LOOPEX DOTDOT YADAYADA
123d08c9
DM
84%token <i_tkval> FUNC0 FUNC1 FUNC UNIOP LSTOP
85%token <i_tkval> RELOP EQOP MULOP ADDOP
86%token <i_tkval> DOLSHARP DO HASHBRACK NOAMP
8d2a5fb4 87%token <i_tkval> LOCAL MY REQUIRE
705fe0e5 88%token <i_tkval> COLONATTR FORMLBRACK FORMRBRACK
f05e27e5 89
727a8fe5 90%type <ival> grammar remember mremember
f05e27e5
DM
91%type <ival> startsub startanonsub startformsub
92/* FIXME for MAD - are these two ival? */
b5a64814 93%type <ival> mintro
f05e27e5 94
8e720305 95%type <opval> stmtseq fullstmt labfullstmt barestmt block mblock else
fad39ff1 96%type <opval> expr term subscripted scalar ary hsh arylen star amper sideff
09b1cffe
Z
97%type <opval> listexpr nexpr texpr iexpr mexpr mnexpr miexpr
98%type <opval> optlistexpr optexpr indirob listop method
7c70caa5 99%type <opval> formname subname proto subbody cont my_scalar formblock
f05e27e5 100%type <opval> subattrlist myattrlist myattrterm myterm
891be019 101%type <opval> termbinop termunop anonymous termdo
705fe0e5 102%type <opval> formstmtseq formline formarg
79072805 103
123d08c9 104%nonassoc <i_tkval> PREC_LOW
fad39ff1
SM
105%nonassoc LOOPEX
106
123d08c9
DM
107%left <i_tkval> OROP DOROP
108%left <i_tkval> ANDOP
109%right <i_tkval> NOTOP
36477c24 110%nonassoc LSTOP LSTOPSUB
123d08c9
DM
111%left <i_tkval> ','
112%right <i_tkval> ASSIGNOP
113%right <i_tkval> '?' ':'
be25f609 114%nonassoc DOTDOT YADAYADA
123d08c9
DM
115%left <i_tkval> OROR DORDOR
116%left <i_tkval> ANDAND
117%left <i_tkval> BITOROP
118%left <i_tkval> BITANDOP
a687059c
LW
119%nonassoc EQOP
120%nonassoc RELOP
36477c24 121%nonassoc UNIOP UNIOPSUB
20515881 122%nonassoc REQUIRE
123d08c9 123%left <i_tkval> SHIFTOP
a687059c
LW
124%left ADDOP
125%left MULOP
123d08c9
DM
126%left <i_tkval> MATCHOP
127%right <i_tkval> '!' '~' UMINUS REFGEN
128%right <i_tkval> POWOP
129%nonassoc <i_tkval> PREINC PREDEC POSTINC POSTDEC
130%left <i_tkval> ARROW
131%nonassoc <i_tkval> ')'
132%left <i_tkval> '('
fad39ff1 133%left '[' '{'
8d063cd8 134
123d08c9 135%token <i_tkval> PEG
7f46837f 136
8d063cd8
LW
137%% /* RULES */
138
28ac2b49 139/* Top-level choice of what kind of thing yyparse was called to parse */
727a8fe5
Z
140grammar : GRAMPROG
141 {
142 PL_parser->expect = XSTATE;
143 }
144 remember stmtseq
145 {
146 newPROG(block_end($3,$4));
147 $$ = 0;
148 }
78cdf107
Z
149 | GRAMEXPR
150 {
151 parser->expect = XTERM;
152 }
09b1cffe 153 optexpr
78cdf107
Z
154 {
155 PL_eval_root = $3;
156 $$ = 0;
157 }
e53d8f76
Z
158 | GRAMBLOCK
159 {
160 parser->expect = XBLOCK;
161 }
162 block
163 {
5f211341 164 PL_pad_reset_pending = TRUE;
8359b381
Z
165 PL_eval_root = $3;
166 $$ = 0;
167 yyunlex();
168 parser->yychar = YYEOF;
169 }
170 | GRAMBARESTMT
171 {
172 parser->expect = XSTATE;
173 }
174 barestmt
175 {
176 PL_pad_reset_pending = TRUE;
e53d8f76
Z
177 PL_eval_root = $3;
178 $$ = 0;
179 yyunlex();
180 parser->yychar = YYEOF;
181 }
9eb5c532 182 | GRAMFULLSTMT
28ac2b49 183 {
9eb5c532
Z
184 parser->expect = XSTATE;
185 }
186 fullstmt
187 {
5f211341 188 PL_pad_reset_pending = TRUE;
9eb5c532 189 PL_eval_root = $3;
28ac2b49
Z
190 $$ = 0;
191 yyunlex();
192 parser->yychar = YYEOF;
193 }
07ffcb73
Z
194 | GRAMSTMTSEQ
195 {
196 parser->expect = XSTATE;
197 }
5f211341 198 stmtseq
07ffcb73
Z
199 {
200 PL_eval_root = $3;
201 $$ = 0;
202 }
203 ;
204
891be019 205/* An ordinary block */
5f211341 206block : '{' remember stmtseq '}'
53a7735b
DM
207 { if (PL_parser->copline > (line_t)IVAL($1))
208 PL_parser->copline = (line_t)IVAL($1);
f05e27e5
DM
209 $$ = block_end($2, $3);
210 TOKEN_GETMAD($1,$$,'{');
211 TOKEN_GETMAD($4,$$,'}');
212 }
a0d0e21e
LW
213 ;
214
7c70caa5 215/* format body */
705fe0e5 216formblock: '=' remember ';' FORMRBRACK formstmtseq ';' '.'
7c70caa5
FC
217 { if (PL_parser->copline > (line_t)IVAL($1))
218 PL_parser->copline = (line_t)IVAL($1);
705fe0e5 219 $$ = block_end($2, $5);
7c70caa5 220 TOKEN_GETMAD($1,$$,'{');
705fe0e5 221 TOKEN_GETMAD($7,$$,'}');
7c70caa5
FC
222 }
223 ;
224
55497cff
PP
225remember: /* NULL */ /* start a full lexical scope */
226 { $$ = block_start(TRUE); }
227 ;
228
5f211341 229mblock : '{' mremember stmtseq '}'
53a7735b
DM
230 { if (PL_parser->copline > (line_t)IVAL($1))
231 PL_parser->copline = (line_t)IVAL($1);
f05e27e5
DM
232 $$ = block_end($2, $3);
233 TOKEN_GETMAD($1,$$,'{');
234 TOKEN_GETMAD($4,$$,'}');
235 }
55497cff
PP
236 ;
237
238mremember: /* NULL */ /* start a partial lexical scope */
239 { $$ = block_start(FALSE); }
8d063cd8
LW
240 ;
241
eae48c89 242/* A sequence of statements in the program */
5f211341 243stmtseq : /* NULL */
bcabcc50 244 { $$ = (OP*)NULL; }
5f211341 245 | stmtseq fullstmt
2fcb4757 246 { $$ = op_append_list(OP_LINESEQ, $1, $2);
3280af22 247 PL_pad_reset_pending = TRUE;
503de470
DM
248 if ($1 && $2)
249 PL_hints |= HINT_BLOCK_SCOPE;
250 }
8d063cd8
LW
251 ;
252
705fe0e5
FC
253/* A sequence of format lines */
254formstmtseq: /* NULL */
255 { $$ = (OP*)NULL; }
256 | formstmtseq formline
257 { $$ = op_append_list(OP_LINESEQ, $1, $2);
258 PL_pad_reset_pending = TRUE;
259 if ($1 && $2)
260 PL_hints |= HINT_BLOCK_SCOPE;
261 }
262 ;
263
8e720305
Z
264/* A statement in the program, including optional labels */
265fullstmt: barestmt
eae48c89 266 {
8e720305
Z
267 if($1) {
268 $$ = newSTATEOP(0, NULL, $1);
eae48c89 269 } else {
8e720305 270 $$ = IF_MAD(newOP(OP_NULL, 0), NULL);
eae48c89
Z
271 }
272 }
8e720305
Z
273 | labfullstmt
274 { $$ = $1; }
275 ;
276
277labfullstmt: LABEL barestmt
278 {
5504e6cf
FC
279 $$ = newSTATEOP(SVf_UTF8
280 * PVAL($1)[strlen(PVAL($1))+1],
281 PVAL($1), $2);
8e720305
Z
282 TOKEN_GETMAD($1,
283 $2 ? cLISTOPx($$)->op_first : $$, 'L');
284 }
285 | LABEL labfullstmt
286 {
5504e6cf
FC
287 $$ = newSTATEOP(SVf_UTF8
288 * PVAL($1)[strlen(PVAL($1))+1],
289 PVAL($1), $2);
8e720305
Z
290 TOKEN_GETMAD($1, cLISTOPx($$)->op_first, 'L');
291 }
eae48c89
Z
292 ;
293
294/* A bare statement, lacking label and other aspects of state op */
295barestmt: PLUGSTMT
0d863452 296 { $$ = $1; }
eae48c89
Z
297 | PEG
298 {
299 $$ = newOP(OP_NULL,0);
300 TOKEN_GETMAD($1,$$,'p');
301 }
7c70caa5 302 | FORMAT startformsub formname formblock
f05e27e5 303 {
eae48c89 304 CV *fmtcv = PL_compcv;
eae48c89
Z
305#ifdef MAD
306 $$ = newFORM($2, $3, $4);
307 prepend_madprops($1->tk_mad, $$, 'F');
308 $1->tk_mad = 0;
309 token_free($1);
310#else
311 newFORM($2, $3, $4);
312 $$ = (OP*)NULL;
313#endif
4a273b91 314 if (CvOUTSIDE(fmtcv) && !CvEVAL(CvOUTSIDE(fmtcv))) {
eae48c89 315 SvREFCNT_inc_simple_void(fmtcv);
cc76b5cc 316 pad_add_anon(fmtcv, OP_NULL);
f05e27e5 317 }
eae48c89 318 }
50278755
FC
319 | SUB subname startsub
320 {
321 if ($2->op_type == OP_CONST) {
322 const char *const name =
323 SvPV_nolen_const(((SVOP*)$2)->op_sv);
324 if (strEQ(name, "BEGIN") || strEQ(name, "END")
764212cf
FC
325 || strEQ(name, "INIT") || strEQ(name, "CHECK")
326 || strEQ(name, "UNITCHECK"))
327 CvSPECIAL_on(PL_compcv);
50278755 328 }
e07561e6
FC
329 else
330 /* State subs inside anonymous subs need to be
331 clonable themselves. */
6d5c2147
FC
332 if (CvANON(CvOUTSIDE(PL_compcv))
333 || CvCLONE(CvOUTSIDE(PL_compcv))
334 || !PadnameIsSTATE(PadlistNAMESARRAY(CvPADLIST(
335 CvOUTSIDE(PL_compcv)
336 ))[$2->op_targ]))
e07561e6 337 CvCLONE_on(PL_compcv);
764212cf
FC
338 PL_parser->in_my = 0;
339 PL_parser->in_my_stash = NULL;
340 }
341 proto subattrlist subbody
eae48c89
Z
342 {
343 SvREFCNT_inc_simple_void(PL_compcv);
344#ifdef MAD
345 {
346 OP* o = newSVOP(OP_ANONCODE, 0,
50278755
FC
347 (SV*)(
348#endif
349 $2->op_type == OP_CONST
350 ? newATTRSUB($3, $2, $5, $6, $7)
351 : newMYSUB($3, $2, $5, $6, $7)
352#ifdef MAD
353 ));
eae48c89
Z
354 $$ = newOP(OP_NULL,0);
355 op_getmad(o,$$,'&');
764212cf
FC
356 op_getmad($2,$$,'n');
357 op_getmad($5,$$,'s');
358 op_getmad($6,$$,'a');
eae48c89 359 token_getmad($1,$$,'d');
764212cf
FC
360 append_madprops($7->op_madprop, $$, 0);
361 $7->op_madprop = 0;
f05e27e5 362 }
eae48c89 363#else
50278755 364 ;
eae48c89
Z
365 $$ = (OP*)NULL;
366#endif
764212cf 367 intro_my();
f05e27e5 368 }
eae48c89 369 | PACKAGE WORD WORD ';'
5f211341 370 {
eae48c89
Z
371#ifdef MAD
372 $$ = package($3);
373 token_getmad($1,$$,'o');
374 if ($2)
375 package_version($2);
376 token_getmad($4,$$,';');
377#else
378 package($3);
379 if ($2)
380 package_version($2);
381 $$ = (OP*)NULL;
382#endif
383 }
384 | USE startsub
385 { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
09b1cffe 386 WORD WORD optlistexpr ';'
eae48c89
Z
387 {
388 SvREFCNT_inc_simple_void(PL_compcv);
389#ifdef MAD
390 $$ = utilize(IVAL($1), $2, $4, $5, $6);
391 token_getmad($1,$$,'o');
392 token_getmad($7,$$,';');
393 if (PL_parser->rsfp_filters &&
394 AvFILLp(PL_parser->rsfp_filters) >= 0)
395 append_madprops(newMADPROP('!', MAD_NULL, NULL, 0), $$, 0);
396#else
397 utilize(IVAL($1), $2, $4, $5, $6);
398 $$ = (OP*)NULL;
399#endif
400 }
417a992d 401 | IF '(' remember mexpr ')' mblock else
eae48c89 402 {
3ad73efd
Z
403 $$ = block_end($3,
404 newCONDOP(0, $4, op_scope($6), $7));
eae48c89
Z
405 TOKEN_GETMAD($1,$$,'I');
406 TOKEN_GETMAD($2,$$,'(');
407 TOKEN_GETMAD($5,$$,')');
408 PL_parser->copline = (line_t)IVAL($1);
409 }
417a992d 410 | UNLESS '(' remember miexpr ')' mblock else
eae48c89 411 {
3ad73efd
Z
412 $$ = block_end($3,
413 newCONDOP(0, $4, op_scope($6), $7));
eae48c89
Z
414 TOKEN_GETMAD($1,$$,'I');
415 TOKEN_GETMAD($2,$$,'(');
416 TOKEN_GETMAD($5,$$,')');
417 PL_parser->copline = (line_t)IVAL($1);
418 }
b5a64814 419 | GIVEN '(' remember mexpr ')' mblock
eae48c89 420 {
b5a64814 421 const PADOFFSET offset = pad_findmy_pvs("$_", 0);
eae48c89 422 $$ = block_end($3,
b5a64814
FC
423 newGIVENOP($4, op_scope($6),
424 offset == NOT_IN_PAD
425 || PAD_COMPNAME_FLAGS_isOUR(offset)
426 ? 0
427 : offset));
eae48c89
Z
428 PL_parser->copline = (line_t)IVAL($1);
429 }
417a992d 430 | WHEN '(' remember mexpr ')' mblock
3ad73efd 431 { $$ = block_end($3, newWHENOP($4, op_scope($6))); }
eae48c89 432 | DEFAULT block
3ad73efd 433 { $$ = newWHENOP(0, op_scope($2)); }
417a992d 434 | WHILE '(' remember texpr ')' mintro mblock cont
eae48c89
Z
435 {
436 $$ = block_end($3,
437 newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
94bf0465 438 $4, $7, $8, $6));
eae48c89
Z
439 TOKEN_GETMAD($1,$$,'W');
440 TOKEN_GETMAD($2,$$,'(');
441 TOKEN_GETMAD($5,$$,')');
442 PL_parser->copline = (line_t)IVAL($1);
443 }
417a992d 444 | UNTIL '(' remember iexpr ')' mintro mblock cont
eae48c89
Z
445 {
446 $$ = block_end($3,
94bf0465
Z
447 newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
448 $4, $7, $8, $6));
eae48c89
Z
449 TOKEN_GETMAD($1,$$,'W');
450 TOKEN_GETMAD($2,$$,'(');
451 TOKEN_GETMAD($5,$$,')');
452 PL_parser->copline = (line_t)IVAL($1);
453 }
417a992d 454 | FOR '(' remember mnexpr ';' texpr ';' mintro mnexpr ')'
eae48c89
Z
455 mblock
456 {
457 OP *initop = IF_MAD($4 ? $4 : newOP(OP_NULL, 0), $4);
458 OP *forop = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
94bf0465 459 scalar($6), $11, $9, $8);
eae48c89
Z
460 if (initop) {
461 forop = op_prepend_elem(OP_LINESEQ, initop,
462 op_append_elem(OP_LINESEQ,
463 newOP(OP_UNSTACK, OPf_SPECIAL),
464 forop));
5f211341 465 }
eae48c89
Z
466 DO_MAD({ forop = newUNOP(OP_NULL, 0, forop); })
467 $$ = block_end($3, forop);
468 TOKEN_GETMAD($1,$$,'3');
469 TOKEN_GETMAD($2,$$,'(');
470 TOKEN_GETMAD($5,$$,'1');
471 TOKEN_GETMAD($7,$$,'2');
472 TOKEN_GETMAD($10,$$,')');
473 PL_parser->copline = (line_t)IVAL($1);
474 }
417a992d 475 | FOR MY remember my_scalar '(' mexpr ')' mblock cont
eae48c89 476 {
94bf0465 477 $$ = block_end($3, newFOROP(0, $4, $6, $8, $9));
eae48c89
Z
478 TOKEN_GETMAD($1,$$,'W');
479 TOKEN_GETMAD($2,$$,'d');
480 TOKEN_GETMAD($5,$$,'(');
481 TOKEN_GETMAD($7,$$,')');
482 PL_parser->copline = (line_t)IVAL($1);
483 }
417a992d 484 | FOR scalar '(' remember mexpr ')' mblock cont
eae48c89 485 {
94bf0465 486 $$ = block_end($4, newFOROP(0,
3ad73efd 487 op_lvalue($2, OP_ENTERLOOP), $5, $7, $8));
eae48c89
Z
488 TOKEN_GETMAD($1,$$,'W');
489 TOKEN_GETMAD($3,$$,'(');
490 TOKEN_GETMAD($6,$$,')');
491 PL_parser->copline = (line_t)IVAL($1);
492 }
417a992d 493 | FOR '(' remember mexpr ')' mblock cont
eae48c89
Z
494 {
495 $$ = block_end($3,
94bf0465 496 newFOROP(0, (OP*)NULL, $4, $6, $7));
eae48c89
Z
497 TOKEN_GETMAD($1,$$,'W');
498 TOKEN_GETMAD($2,$$,'(');
499 TOKEN_GETMAD($5,$$,')');
500 PL_parser->copline = (line_t)IVAL($1);
501 }
502 | block cont
503 {
504 /* a block is a loop that happens once */
505 $$ = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
94bf0465 506 (OP*)NULL, $1, $2, 0);
eae48c89
Z
507 }
508 | PACKAGE WORD WORD '{' remember
509 {
eae48c89 510 package($3);
eae48c89 511 if ($2) {
eae48c89 512 package_version($2);
eae48c89
Z
513 }
514 }
515 stmtseq '}'
516 {
517 /* a block is a loop that happens once */
94bf0465 518 $$ = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
eae48c89 519 (OP*)NULL, block_end($5, $7), (OP*)NULL, 0);
eae48c89
Z
520 TOKEN_GETMAD($4,$$,'{');
521 TOKEN_GETMAD($8,$$,'}');
522 if (PL_parser->copline > (line_t)IVAL($4))
523 PL_parser->copline = (line_t)IVAL($4);
524 }
525 | sideff ';'
526 {
527 PL_parser->expect = XSTATE;
528 $$ = $1;
529 TOKEN_GETMAD($2,$$,';');
530 }
531 | ';'
532 {
533 PL_parser->expect = XSTATE;
534 $$ = IF_MAD(newOP(OP_NULL, 0), (OP*)NULL);
535 TOKEN_GETMAD($1,$$,';');
536 PL_parser->copline = NOLINE;
5f211341 537 }
8d063cd8
LW
538 ;
539
705fe0e5
FC
540/* Format line */
541formline: THING formarg
542 { OP *list;
543 if ($2) {
544 OP *term = $2;
545 DO_MAD(term = newUNOP(OP_NULL, 0, term));
546 list = op_append_elem(OP_LIST, $1, term);
547 }
548 else {
549#ifdef MAD
550 OP *op = newNULLLIST();
551 list = op_append_elem(OP_LIST, $1, op);
552#else
553 list = $1;
554#endif
555 }
556 if (PL_parser->copline == NOLINE)
557 PL_parser->copline = CopLINE(PL_curcop)-1;
558 else PL_parser->copline--;
559 $$ = newSTATEOP(0, NULL,
560 convert(OP_FORMLINE, 0, list));
561 }
562 ;
563
564formarg : /* NULL */
565 { $$ = NULL; }
566 | FORMLBRACK stmtseq FORMRBRACK
567 { $$ = op_unscope($2); }
568 ;
569
891be019 570/* An expression which may have a side-effect */
a687059c 571sideff : error
bcabcc50 572 { $$ = (OP*)NULL; }
a687059c 573 | expr
79072805 574 { $$ = $1; }
a687059c 575 | expr IF expr
f05e27e5
DM
576 { $$ = newLOGOP(OP_AND, 0, $3, $1);
577 TOKEN_GETMAD($2,$$,'i');
578 }
a687059c 579 | expr UNLESS expr
f05e27e5
DM
580 { $$ = newLOGOP(OP_OR, 0, $3, $1);
581 TOKEN_GETMAD($2,$$,'i');
582 }
a687059c 583 | expr WHILE expr
f05e27e5
DM
584 { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1);
585 TOKEN_GETMAD($2,$$,'w');
586 }
55497cff 587 | expr UNTIL iexpr
f05e27e5
DM
588 { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1);
589 TOKEN_GETMAD($2,$$,'w');
590 }
ecca16b0 591 | expr FOR expr
94bf0465 592 { $$ = newFOROP(0, (OP*)NULL, $3, $1, (OP*)NULL);
eae48c89 593 TOKEN_GETMAD($2,$$,'w');
94bf0465 594 PL_parser->copline = (line_t)IVAL($2);
f05e27e5 595 }
f20dcd76 596 | expr WHEN expr
3ad73efd 597 { $$ = newWHENOP($3, op_scope($1)); }
79072805
LW
598 ;
599
891be019 600/* else and elsif blocks */
79072805 601else : /* NULL */
bcabcc50 602 { $$ = (OP*)NULL; }
55497cff 603 | ELSE mblock
3ad73efd
Z
604 {
605 ($2)->op_flags |= OPf_PARENS;
606 $$ = op_scope($2);
f05e27e5
DM
607 TOKEN_GETMAD($1,$$,'o');
608 }
417a992d 609 | ELSIF '(' mexpr ')' mblock else
53a7735b 610 { PL_parser->copline = (line_t)IVAL($1);
3ad73efd
Z
611 $$ = newCONDOP(0,
612 newSTATEOP(OPf_SPECIAL,NULL,$3),
613 op_scope($5), $6);
614 PL_hints |= HINT_BLOCK_SCOPE;
f05e27e5
DM
615 TOKEN_GETMAD($1,$$,'I');
616 TOKEN_GETMAD($2,$$,'(');
617 TOKEN_GETMAD($4,$$,')');
618 }
79072805
LW
619 ;
620
891be019 621/* Continue blocks */
79072805 622cont : /* NULL */
bcabcc50 623 { $$ = (OP*)NULL; }
79072805 624 | CONTINUE block
3ad73efd
Z
625 {
626 $$ = op_scope($2);
f05e27e5
DM
627 TOKEN_GETMAD($1,$$,'o');
628 }
79072805
LW
629 ;
630
a034e688
DM
631/* determine whether there are any new my declarations */
632mintro : /* NULL */
633 { $$ = (PL_min_intro_pending &&
634 PL_max_intro_pending >= PL_min_intro_pending);
635 intro_my(); }
636
891be019 637/* Normal expression */
8d063cd8 638nexpr : /* NULL */
bcabcc50 639 { $$ = (OP*)NULL; }
8d063cd8
LW
640 | sideff
641 ;
642
891be019 643/* Boolean expression */
8d063cd8 644texpr : /* NULL means true */
f05e27e5
DM
645 { YYSTYPE tmplval;
646 (void)scan_num("1", &tmplval);
647 $$ = tmplval.opval; }
8d063cd8
LW
648 | expr
649 ;
650
891be019 651/* Inverted boolean expression */
55497cff
PP
652iexpr : expr
653 { $$ = invert(scalar($1)); }
654 ;
655
891be019 656/* Expression with its own lexical scope */
55497cff 657mexpr : expr
bbce6d69
PP
658 { $$ = $1; intro_my(); }
659 ;
660
661mnexpr : nexpr
662 { $$ = $1; intro_my(); }
55497cff
PP
663 ;
664
55497cff 665miexpr : iexpr
bbce6d69 666 { $$ = $1; intro_my(); }
55497cff
PP
667 ;
668
44a8e56a 669formname: WORD { $$ = $1; }
bcabcc50 670 | /* NULL */ { $$ = (OP*)NULL; }
8d063cd8
LW
671 ;
672
fa83b5b6 673startsub: /* NULL */ /* start a regular subroutine scope */
a8ff2fa6
DM
674 { $$ = start_subparse(FALSE, 0);
675 SAVEFREESV(PL_compcv); }
f05e27e5 676
28757baa
PP
677 ;
678
679startanonsub: /* NULL */ /* start an anonymous subroutine scope */
a8ff2fa6
DM
680 { $$ = start_subparse(FALSE, CVf_ANON);
681 SAVEFREESV(PL_compcv); }
28757baa
PP
682 ;
683
44a8e56a 684startformsub: /* NULL */ /* start a format subroutine scope */
a8ff2fa6
DM
685 { $$ = start_subparse(TRUE, 0);
686 SAVEFREESV(PL_compcv); }
44a8e56a
PP
687 ;
688
891be019 689/* Name of a subroutine - must be a bareword, could be special */
50278755 690subname : WORD
4210d3f1 691 | PRIVATEREF
a0d0e21e
LW
692 ;
693
891be019 694/* Subroutine prototype */
4633a7c4 695proto : /* NULL */
bcabcc50 696 { $$ = (OP*)NULL; }
4633a7c4
LW
697 | THING
698 ;
28757baa 699
891be019 700/* Optional list of subroutine attributes */
09bef843 701subattrlist: /* NULL */
bcabcc50 702 { $$ = (OP*)NULL; }
09bef843 703 | COLONATTR THING
f05e27e5
DM
704 { $$ = $2;
705 TOKEN_GETMAD($1,$$,':');
706 }
09bef843 707 | COLONATTR
f05e27e5
DM
708 { $$ = IF_MAD(
709 newOP(OP_NULL, 0),
bcabcc50 710 (OP*)NULL
f05e27e5
DM
711 );
712 TOKEN_GETMAD($1,$$,':');
713 }
09bef843
SB
714 ;
715
891be019 716/* List of attributes for a "my" variable declaration */
09bef843 717myattrlist: COLONATTR THING
f05e27e5
DM
718 { $$ = $2;
719 TOKEN_GETMAD($1,$$,':');
720 }
09bef843 721 | COLONATTR
f05e27e5
DM
722 { $$ = IF_MAD(
723 newOP(OP_NULL, 0),
bcabcc50 724 (OP*)NULL
f05e27e5
DM
725 );
726 TOKEN_GETMAD($1,$$,':');
727 }
09bef843
SB
728 ;
729
891be019 730/* Subroutine body - either null or a block */
28757baa 731subbody : block { $$ = $1; }
f05e27e5
DM
732 | ';' { $$ = IF_MAD(
733 newOP(OP_NULL,0),
bcabcc50 734 (OP*)NULL
f05e27e5 735 );
53a7735b 736 PL_parser->expect = XSTATE;
f05e27e5
DM
737 TOKEN_GETMAD($1,$$,';');
738 }
8d063cd8
LW
739 ;
740
891be019 741/* Ordinary expressions; logical combinations */
a0d0e21e 742expr : expr ANDOP expr
f05e27e5
DM
743 { $$ = newLOGOP(OP_AND, 0, $1, $3);
744 TOKEN_GETMAD($2,$$,'o');
745 }
a0d0e21e 746 | expr OROP expr
f05e27e5
DM
747 { $$ = newLOGOP(IVAL($2), 0, $1, $3);
748 TOKEN_GETMAD($2,$$,'o');
749 }
c963b151 750 | expr DOROP expr
f05e27e5
DM
751 { $$ = newLOGOP(OP_DOR, 0, $1, $3);
752 TOKEN_GETMAD($2,$$,'o');
753 }
09b1cffe 754 | listexpr %prec PREC_LOW
a0d0e21e
LW
755 ;
756
891be019 757/* Expressions are a list of terms joined by commas */
09b1cffe 758listexpr: listexpr ','
f05e27e5
DM
759 {
760#ifdef MAD
761 OP* op = newNULLLIST();
762 token_getmad($2,op,',');
2fcb4757 763 $$ = op_append_elem(OP_LIST, $1, op);
f05e27e5
DM
764#else
765 $$ = $1;
766#endif
767 }
09b1cffe 768 | listexpr ',' term
f05e27e5 769 {
29522234 770 OP* term = $3;
f05e27e5 771 DO_MAD(
29522234
DM
772 term = newUNOP(OP_NULL, 0, term);
773 token_getmad($2,term,',');
f05e27e5 774 )
2fcb4757 775 $$ = op_append_elem(OP_LIST, $1, term);
f05e27e5 776 }
fad39ff1 777 | term %prec PREC_LOW
8d063cd8
LW
778 ;
779
891be019 780/* List operators */
09b1cffe 781listop : LSTOP indirob listexpr /* map {...} @args or print $fh @args */
f05e27e5 782 { $$ = convert(IVAL($1), OPf_STACKED,
2fcb4757 783 op_prepend_elem(OP_LIST, newGVREF(IVAL($1),$2), $3) );
f05e27e5
DM
784 TOKEN_GETMAD($1,$$,'o');
785 }
891be019 786 | FUNC '(' indirob expr ')' /* print ($fh @args */
f05e27e5 787 { $$ = convert(IVAL($1), OPf_STACKED,
2fcb4757 788 op_prepend_elem(OP_LIST, newGVREF(IVAL($1),$3), $4) );
f05e27e5
DM
789 TOKEN_GETMAD($1,$$,'o');
790 TOKEN_GETMAD($2,$$,'(');
791 TOKEN_GETMAD($5,$$,')');
792 }
417a992d 793 | term ARROW method '(' optexpr ')' /* $foo->bar(list) */
4633a7c4 794 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
2fcb4757
Z
795 op_append_elem(OP_LIST,
796 op_prepend_elem(OP_LIST, scalar($1), $5),
f05e27e5
DM
797 newUNOP(OP_METHOD, 0, $3)));
798 TOKEN_GETMAD($2,$$,'A');
799 TOKEN_GETMAD($4,$$,'(');
800 TOKEN_GETMAD($6,$$,')');
801 }
891be019 802 | term ARROW method /* $foo->bar */
b1524f17 803 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
2fcb4757 804 op_append_elem(OP_LIST, scalar($1),
f05e27e5
DM
805 newUNOP(OP_METHOD, 0, $3)));
806 TOKEN_GETMAD($2,$$,'A');
807 }
09b1cffe 808 | METHOD indirob optlistexpr /* new Class @args */
4633a7c4 809 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
2fcb4757
Z
810 op_append_elem(OP_LIST,
811 op_prepend_elem(OP_LIST, $2, $3),
f05e27e5
DM
812 newUNOP(OP_METHOD, 0, $1)));
813 }
09b1cffe 814 | FUNCMETH indirob '(' optexpr ')' /* method $object (@args) */
4633a7c4 815 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
2fcb4757
Z
816 op_append_elem(OP_LIST,
817 op_prepend_elem(OP_LIST, $2, $4),
f05e27e5
DM
818 newUNOP(OP_METHOD, 0, $1)));
819 TOKEN_GETMAD($3,$$,'(');
820 TOKEN_GETMAD($5,$$,')');
821 }
09b1cffe 822 | LSTOP optlistexpr /* print @args */
f05e27e5
DM
823 { $$ = convert(IVAL($1), 0, $2);
824 TOKEN_GETMAD($1,$$,'o');
825 }
09b1cffe 826 | FUNC '(' optexpr ')' /* print (@args) */
f05e27e5
DM
827 { $$ = convert(IVAL($1), 0, $3);
828 TOKEN_GETMAD($1,$$,'o');
829 TOKEN_GETMAD($2,$$,'(');
830 TOKEN_GETMAD($4,$$,')');
831 }
718a7425 832 | LSTOPSUB startanonsub block /* sub f(&@); f { foo } ... */
5a5094bd 833 { SvREFCNT_inc_simple_void(PL_compcv);
bcabcc50 834 $<opval>$ = newANONATTRSUB($2, 0, (OP*)NULL, $3); }
09b1cffe 835 optlistexpr %prec LSTOP /* ... @bar */
4633a7c4 836 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
2fcb4757
Z
837 op_append_elem(OP_LIST,
838 op_prepend_elem(OP_LIST, $<opval>4, $5), $1));
f05e27e5 839 }
a687059c
LW
840 ;
841
891be019 842/* Names of methods. May use $object->$methodname */
a0d0e21e
LW
843method : METHOD
844 | scalar
845 ;
846
891be019
SC
847/* Some kind of subscripted expression */
848subscripted: star '{' expr ';' '}' /* *main::{something} */
849 /* In this and all the hash accessors, ';' is
850 * provided by the tokeniser */
264e1af3 851 { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3));
53a7735b 852 PL_parser->expect = XOPERATOR;
f05e27e5
DM
853 TOKEN_GETMAD($2,$$,'{');
854 TOKEN_GETMAD($4,$$,';');
855 TOKEN_GETMAD($5,$$,'}');
856 }
891be019 857 | scalar '[' expr ']' /* $array[$element] */
f05e27e5
DM
858 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3));
859 TOKEN_GETMAD($2,$$,'[');
860 TOKEN_GETMAD($4,$$,']');
861 }
891be019 862 | term ARROW '[' expr ']' /* somearef->[$element] */
fad39ff1
SM
863 { $$ = newBINOP(OP_AELEM, 0,
864 ref(newAVREF($1),OP_RV2AV),
f05e27e5
DM
865 scalar($4));
866 TOKEN_GETMAD($2,$$,'a');
867 TOKEN_GETMAD($3,$$,'[');
868 TOKEN_GETMAD($5,$$,']');
869 }
891be019 870 | subscripted '[' expr ']' /* $foo->[$bar]->[$baz] */
fad39ff1
SM
871 { $$ = newBINOP(OP_AELEM, 0,
872 ref(newAVREF($1),OP_RV2AV),
f05e27e5
DM
873 scalar($3));
874 TOKEN_GETMAD($2,$$,'[');
875 TOKEN_GETMAD($4,$$,']');
876 }
5a63ceef 877 | scalar '{' expr ';' '}' /* $foo{bar();} */
fad39ff1 878 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
53a7735b 879 PL_parser->expect = XOPERATOR;
f05e27e5
DM
880 TOKEN_GETMAD($2,$$,'{');
881 TOKEN_GETMAD($4,$$,';');
882 TOKEN_GETMAD($5,$$,'}');
883 }
891be019 884 | term ARROW '{' expr ';' '}' /* somehref->{bar();} */
fad39ff1
SM
885 { $$ = newBINOP(OP_HELEM, 0,
886 ref(newHVREF($1),OP_RV2HV),
887 jmaybe($4));
53a7735b 888 PL_parser->expect = XOPERATOR;
f05e27e5
DM
889 TOKEN_GETMAD($2,$$,'a');
890 TOKEN_GETMAD($3,$$,'{');
891 TOKEN_GETMAD($5,$$,';');
892 TOKEN_GETMAD($6,$$,'}');
893 }
891be019 894 | subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */
fad39ff1
SM
895 { $$ = newBINOP(OP_HELEM, 0,
896 ref(newHVREF($1),OP_RV2HV),
897 jmaybe($3));
53a7735b 898 PL_parser->expect = XOPERATOR;
f05e27e5
DM
899 TOKEN_GETMAD($2,$$,'{');
900 TOKEN_GETMAD($4,$$,';');
901 TOKEN_GETMAD($5,$$,'}');
902 }
891be019 903 | term ARROW '(' ')' /* $subref->() */
fad39ff1 904 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
f05e27e5
DM
905 newCVREF(0, scalar($1)));
906 TOKEN_GETMAD($2,$$,'a');
907 TOKEN_GETMAD($3,$$,'(');
908 TOKEN_GETMAD($4,$$,')');
909 }
891be019 910 | term ARROW '(' expr ')' /* $subref->(@args) */
fad39ff1 911 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
2fcb4757 912 op_append_elem(OP_LIST, $4,
f05e27e5
DM
913 newCVREF(0, scalar($1))));
914 TOKEN_GETMAD($2,$$,'a');
915 TOKEN_GETMAD($3,$$,'(');
916 TOKEN_GETMAD($5,$$,')');
917 }
fad39ff1 918
417a992d 919 | subscripted '(' expr ')' /* $foo->{bar}->(@args) */
fad39ff1 920 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
2fcb4757 921 op_append_elem(OP_LIST, $3,
f05e27e5
DM
922 newCVREF(0, scalar($1))));
923 TOKEN_GETMAD($2,$$,'(');
924 TOKEN_GETMAD($4,$$,')');
925 }
417a992d 926 | subscripted '(' ')' /* $foo->{bar}->() */
fad39ff1 927 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
f05e27e5
DM
928 newCVREF(0, scalar($1)));
929 TOKEN_GETMAD($2,$$,'(');
930 TOKEN_GETMAD($3,$$,')');
931 }
9a9798c2 932 | '(' expr ')' '[' expr ']' /* list slice */
f05e27e5
DM
933 { $$ = newSLICEOP(0, $5, $2);
934 TOKEN_GETMAD($1,$$,'(');
935 TOKEN_GETMAD($3,$$,')');
936 TOKEN_GETMAD($4,$$,'[');
937 TOKEN_GETMAD($6,$$,']');
938 }
ea25a9b2
Z
939 | QWLIST '[' expr ']' /* list literal slice */
940 { $$ = newSLICEOP(0, $3, $1);
941 TOKEN_GETMAD($2,$$,'[');
942 TOKEN_GETMAD($4,$$,']');
943 }
9a9798c2 944 | '(' ')' '[' expr ']' /* empty list slice! */
bcabcc50 945 { $$ = newSLICEOP(0, $4, (OP*)NULL);
f05e27e5
DM
946 TOKEN_GETMAD($1,$$,'(');
947 TOKEN_GETMAD($2,$$,')');
948 TOKEN_GETMAD($3,$$,'[');
949 TOKEN_GETMAD($5,$$,']');
950 }
891be019 951 ;
fad39ff1 952
891be019 953/* Binary operators between terms */
f05e27e5
DM
954termbinop: term ASSIGNOP term /* $x = $y */
955 { $$ = newASSIGNOP(OPf_STACKED, $1, IVAL($2), $3);
956 TOKEN_GETMAD($2,$$,'o');
957 }
891be019 958 | term POWOP term /* $x ** $y */
f05e27e5
DM
959 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
960 TOKEN_GETMAD($2,$$,'o');
961 }
891be019 962 | term MULOP term /* $x * $y, $x x $y */
f05e27e5 963 { if (IVAL($2) != OP_REPEAT)
79072805 964 scalar($1);
f05e27e5
DM
965 $$ = newBINOP(IVAL($2), 0, $1, scalar($3));
966 TOKEN_GETMAD($2,$$,'o');
967 }
891be019 968 | term ADDOP term /* $x + $y */
f05e27e5
DM
969 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
970 TOKEN_GETMAD($2,$$,'o');
971 }
891be019 972 | term SHIFTOP term /* $x >> $y, $x << $y */
f05e27e5
DM
973 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
974 TOKEN_GETMAD($2,$$,'o');
975 }
891be019 976 | term RELOP term /* $x > $y, etc. */
f05e27e5
DM
977 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
978 TOKEN_GETMAD($2,$$,'o');
979 }
891be019 980 | term EQOP term /* $x == $y, $x eq $y */
f05e27e5
DM
981 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
982 TOKEN_GETMAD($2,$$,'o');
983 }
891be019 984 | term BITANDOP term /* $x & $y */
f05e27e5
DM
985 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
986 TOKEN_GETMAD($2,$$,'o');
987 }
891be019 988 | term BITOROP term /* $x | $y */
f05e27e5
DM
989 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
990 TOKEN_GETMAD($2,$$,'o');
991 }
891be019 992 | term DOTDOT term /* $x..$y, $x...$y */
f05e27e5
DM
993 {
994 $$ = newRANGE(IVAL($2), scalar($1), scalar($3));
035e2bcc 995 DO_MAD({
f05e27e5
DM
996 UNOP *op;
997 op = (UNOP*)$$;
998 op = (UNOP*)op->op_first; /* get to flop */
999 op = (UNOP*)op->op_first; /* get to flip */
1000 op = (UNOP*)op->op_first; /* get to range */
1001 token_getmad($2,(OP*)op,'o');
04ff073f 1002 });
f05e27e5 1003 }
891be019 1004 | term ANDAND term /* $x && $y */
f05e27e5
DM
1005 { $$ = newLOGOP(OP_AND, 0, $1, $3);
1006 TOKEN_GETMAD($2,$$,'o');
1007 }
891be019 1008 | term OROR term /* $x || $y */
f05e27e5
DM
1009 { $$ = newLOGOP(OP_OR, 0, $1, $3);
1010 TOKEN_GETMAD($2,$$,'o');
1011 }
c963b151 1012 | term DORDOR term /* $x // $y */
f05e27e5
DM
1013 { $$ = newLOGOP(OP_DOR, 0, $1, $3);
1014 TOKEN_GETMAD($2,$$,'o');
1015 }
891be019 1016 | term MATCHOP term /* $x =~ /$y/ */
f05e27e5
DM
1017 { $$ = bind_match(IVAL($2), $1, $3);
1018 TOKEN_GETMAD($2,
1019 ($$->op_type == OP_NOT
1020 ? ((UNOP*)$$)->op_first : $$),
1021 '~');
1022 }
891be019 1023 ;
8d063cd8 1024
891be019
SC
1025/* Unary operators and terms */
1026termunop : '-' term %prec UMINUS /* -$x */
f05e27e5
DM
1027 { $$ = newUNOP(OP_NEGATE, 0, scalar($2));
1028 TOKEN_GETMAD($1,$$,'o');
1029 }
891be019 1030 | '+' term %prec UMINUS /* +$x */
f05e27e5
DM
1031 { $$ = IF_MAD(
1032 newUNOP(OP_NULL, 0, $2),
1033 $2
1034 );
1035 TOKEN_GETMAD($1,$$,'+');
1036 }
891be019 1037 | '!' term /* !$x */
f05e27e5
DM
1038 { $$ = newUNOP(OP_NOT, 0, scalar($2));
1039 TOKEN_GETMAD($1,$$,'o');
1040 }
891be019 1041 | '~' term /* ~$x */
f05e27e5
DM
1042 { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));
1043 TOKEN_GETMAD($1,$$,'o');
1044 }
891be019 1045 | term POSTINC /* $x++ */
79072805 1046 { $$ = newUNOP(OP_POSTINC, 0,
3ad73efd 1047 op_lvalue(scalar($1), OP_POSTINC));
f05e27e5
DM
1048 TOKEN_GETMAD($2,$$,'o');
1049 }
891be019 1050 | term POSTDEC /* $x-- */
79072805 1051 { $$ = newUNOP(OP_POSTDEC, 0,
3ad73efd 1052 op_lvalue(scalar($1), OP_POSTDEC));
f05e27e5
DM
1053 TOKEN_GETMAD($2,$$,'o');
1054 }
891be019 1055 | PREINC term /* ++$x */
79072805 1056 { $$ = newUNOP(OP_PREINC, 0,
3ad73efd 1057 op_lvalue(scalar($2), OP_PREINC));
f05e27e5
DM
1058 TOKEN_GETMAD($1,$$,'o');
1059 }
891be019 1060 | PREDEC term /* --$x */
79072805 1061 { $$ = newUNOP(OP_PREDEC, 0,
3ad73efd 1062 op_lvalue(scalar($2), OP_PREDEC));
f05e27e5
DM
1063 TOKEN_GETMAD($1,$$,'o');
1064 }
891be019
SC
1065
1066 ;
1067
1068/* Constructors for anonymous data */
1069anonymous: '[' expr ']'
f05e27e5
DM
1070 { $$ = newANONLIST($2);
1071 TOKEN_GETMAD($1,$$,'[');
1072 TOKEN_GETMAD($3,$$,']');
1073 }
891be019 1074 | '[' ']'
bcabcc50 1075 { $$ = newANONLIST((OP*)NULL);
f05e27e5
DM
1076 TOKEN_GETMAD($1,$$,'[');
1077 TOKEN_GETMAD($2,$$,']');
1078 }
891be019 1079 | HASHBRACK expr ';' '}' %prec '(' /* { foo => "Bar" } */
f05e27e5
DM
1080 { $$ = newANONHASH($2);
1081 TOKEN_GETMAD($1,$$,'{');
1082 TOKEN_GETMAD($3,$$,';');
1083 TOKEN_GETMAD($4,$$,'}');
1084 }
891be019 1085 | HASHBRACK ';' '}' %prec '(' /* { } (';' by tokener) */
bcabcc50 1086 { $$ = newANONHASH((OP*)NULL);
f05e27e5
DM
1087 TOKEN_GETMAD($1,$$,'{');
1088 TOKEN_GETMAD($2,$$,';');
1089 TOKEN_GETMAD($3,$$,'}');
1090 }
718a7425 1091 | ANONSUB startanonsub proto subattrlist block %prec '('
5a5094bd 1092 { SvREFCNT_inc_simple_void(PL_compcv);
718a7425 1093 $$ = newANONATTRSUB($2, $3, $4, $5);
f05e27e5 1094 TOKEN_GETMAD($1,$$,'o');
718a7425
DM
1095 OP_GETMAD($3,$$,'s');
1096 OP_GETMAD($4,$$,'a');
f05e27e5 1097 }
891be019
SC
1098
1099 ;
1100
1101/* Things called with "do" */
1102termdo : DO term %prec UNIOP /* do $filename */
123d08c9 1103 { $$ = dofile($2, IVAL($1));
f05e27e5
DM
1104 TOKEN_GETMAD($1,$$,'o');
1105 }
891be019 1106 | DO block %prec '(' /* do { code */
3ad73efd 1107 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, op_scope($2));
f05e27e5
DM
1108 TOKEN_GETMAD($1,$$,'D');
1109 }
73f3e228 1110 | DO subname '(' ')' /* do somesub() */
891be019
SC
1111 { $$ = newUNOP(OP_ENTERSUB,
1112 OPf_SPECIAL|OPf_STACKED,
2fcb4757 1113 op_prepend_elem(OP_LIST,
891be019
SC
1114 scalar(newCVREF(
1115 (OPpENTERSUB_AMPER<<8),
1116 scalar($2)
bcabcc50 1117 )),(OP*)NULL)); dep();
f05e27e5
DM
1118 TOKEN_GETMAD($1,$$,'o');
1119 TOKEN_GETMAD($3,$$,'(');
1120 TOKEN_GETMAD($4,$$,')');
1121 }
73f3e228 1122 | DO subname '(' expr ')' /* do somesub(@args) */
891be019
SC
1123 { $$ = newUNOP(OP_ENTERSUB,
1124 OPf_SPECIAL|OPf_STACKED,
2fcb4757 1125 op_append_elem(OP_LIST,
891be019
SC
1126 $4,
1127 scalar(newCVREF(
1128 (OPpENTERSUB_AMPER<<8),
1129 scalar($2)
f05e27e5
DM
1130 )))); dep();
1131 TOKEN_GETMAD($1,$$,'o');
1132 TOKEN_GETMAD($3,$$,'(');
1133 TOKEN_GETMAD($5,$$,')');
1134 }
417a992d 1135 | DO scalar '(' ')' /* do $subref () */
891be019 1136 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
2fcb4757 1137 op_prepend_elem(OP_LIST,
bcabcc50 1138 scalar(newCVREF(0,scalar($2))), (OP*)NULL)); dep();
f05e27e5
DM
1139 TOKEN_GETMAD($1,$$,'o');
1140 TOKEN_GETMAD($3,$$,'(');
1141 TOKEN_GETMAD($4,$$,')');
1142 }
417a992d 1143 | DO scalar '(' expr ')' /* do $subref (@args) */
891be019 1144 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
2fcb4757 1145 op_prepend_elem(OP_LIST,
891be019 1146 $4,
f05e27e5
DM
1147 scalar(newCVREF(0,scalar($2))))); dep();
1148 TOKEN_GETMAD($1,$$,'o');
1149 TOKEN_GETMAD($3,$$,'(');
1150 TOKEN_GETMAD($5,$$,')');
1151 }
891be019
SC
1152
1153 ;
1154
1155term : termbinop
1156 | termunop
1157 | anonymous
1158 | termdo
e9bdcc27 1159 | term '?' term ':' term
f05e27e5
DM
1160 { $$ = newCONDOP(0, $1, $3, $5);
1161 TOKEN_GETMAD($2,$$,'?');
1162 TOKEN_GETMAD($4,$$,':');
1163 }
891be019 1164 | REFGEN term /* \$x, \@y, \%z */
3ad73efd 1165 { $$ = newUNOP(OP_REFGEN, 0, op_lvalue($2,OP_REFGEN));
f05e27e5
DM
1166 TOKEN_GETMAD($1,$$,'o');
1167 }
09bef843
SB
1168 | myattrterm %prec UNIOP
1169 { $$ = $1; }
1170 | LOCAL term %prec UNIOP
f05e27e5 1171 { $$ = localize($2,IVAL($1));
dde83331 1172 TOKEN_GETMAD($1,$$,'k');
f05e27e5 1173 }
a0d0e21e 1174 | '(' expr ')'
f05e27e5
DM
1175 { $$ = sawparens(IF_MAD(newUNOP(OP_NULL,0,$2), $2));
1176 TOKEN_GETMAD($1,$$,'(');
1177 TOKEN_GETMAD($3,$$,')');
1178 }
ea25a9b2
Z
1179 | QWLIST
1180 { $$ = IF_MAD(newUNOP(OP_NULL,0,$1), $1); }
8d063cd8 1181 | '(' ')'
f05e27e5
DM
1182 { $$ = sawparens(newNULLLIST());
1183 TOKEN_GETMAD($1,$$,'(');
1184 TOKEN_GETMAD($2,$$,')');
1185 }
79072805 1186 | scalar %prec '('
8d063cd8 1187 { $$ = $1; }
79072805 1188 | star %prec '('
8d063cd8 1189 { $$ = $1; }
79072805 1190 | hsh %prec '('
8d063cd8 1191 { $$ = $1; }
79072805 1192 | ary %prec '('
8d063cd8 1193 { $$ = $1; }
891be019 1194 | arylen %prec '(' /* $#x, $#{ something } */
79072805 1195 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
fad39ff1
SM
1196 | subscripted
1197 { $$ = $1; }
891be019 1198 | ary '[' expr ']' /* array slice */
2fcb4757 1199 { $$ = op_prepend_elem(OP_ASLICE,
79072805 1200 newOP(OP_PUSHMARK, 0),
79072805
LW
1201 newLISTOP(OP_ASLICE, 0,
1202 list($3),
f05e27e5 1203 ref($1, OP_ASLICE)));
429a2555
FC
1204 if ($$ && $1)
1205 $$->op_private |=
1206 $1->op_private & OPpSLICEWARNING;
f05e27e5
DM
1207 TOKEN_GETMAD($2,$$,'[');
1208 TOKEN_GETMAD($4,$$,']');
1209 }
6dd3e0f2
RZ
1210 | hsh '[' expr ']' /* array key/value slice */
1211 { $$ = op_prepend_elem(OP_KVASLICE,
1212 newOP(OP_PUSHMARK, 0),
1213 newLISTOP(OP_KVASLICE, 0,
1214 list($3),
1215 ref(oopsAV($1), OP_KVASLICE)));
95a31aad
FC
1216 if ($$ && $1)
1217 $$->op_private |=
1218 $1->op_private & OPpSLICEWARNING;
6dd3e0f2
RZ
1219 TOKEN_GETMAD($2,$$,'[');
1220 TOKEN_GETMAD($4,$$,']');
1221 }
891be019 1222 | ary '{' expr ';' '}' /* @hash{@keys} */
2fcb4757 1223 { $$ = op_prepend_elem(OP_HSLICE,
79072805 1224 newOP(OP_PUSHMARK, 0),
79072805
LW
1225 newLISTOP(OP_HSLICE, 0,
1226 list($3),
a0d0e21e 1227 ref(oopsHV($1), OP_HSLICE)));
429a2555
FC
1228 if ($$ && $1)
1229 $$->op_private |=
1230 $1->op_private & OPpSLICEWARNING;
53a7735b 1231 PL_parser->expect = XOPERATOR;
f05e27e5
DM
1232 TOKEN_GETMAD($2,$$,'{');
1233 TOKEN_GETMAD($4,$$,';');
1234 TOKEN_GETMAD($5,$$,'}');
1235 }
5cae3edb
RZ
1236 | hsh '{' expr ';' '}' /* %hash{@keys} */
1237 { $$ = op_prepend_elem(OP_KVHSLICE,
1238 newOP(OP_PUSHMARK, 0),
1239 newLISTOP(OP_KVHSLICE, 0,
1240 list($3),
1241 ref($1, OP_KVHSLICE)));
95a31aad
FC
1242 if ($$ && $1)
1243 $$->op_private |=
1244 $1->op_private & OPpSLICEWARNING;
5cae3edb
RZ
1245 PL_parser->expect = XOPERATOR;
1246 TOKEN_GETMAD($2,$$,'{');
1247 TOKEN_GETMAD($4,$$,';');
1248 TOKEN_GETMAD($5,$$,'}');
1249 }
79072805
LW
1250 | THING %prec '('
1251 { $$ = $1; }
891be019 1252 | amper /* &foo; */
c07a80fd 1253 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
fb602e32 1254 | amper '(' ')' /* &foo() or foo() */
f05e27e5
DM
1255 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1));
1256 TOKEN_GETMAD($2,$$,'(');
1257 TOKEN_GETMAD($3,$$,')');
1258 }
fb602e32 1259 | amper '(' expr ')' /* &foo(@args) or foo(@args) */
f05e27e5
DM
1260 {
1261 $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
2fcb4757 1262 op_append_elem(OP_LIST, $3, scalar($1)));
035e2bcc 1263 DO_MAD({
f05e27e5
DM
1264 OP* op = $$;
1265 if (op->op_type == OP_CONST) { /* defeat const fold */
1266 op = (OP*)op->op_madprop->mad_val;
1267 }
1268 token_getmad($2,op,'(');
1269 token_getmad($4,op,')');
04ff073f 1270 });
f05e27e5 1271 }
fb602e32 1272 | NOAMP subname optlistexpr /* foo @args (no parens) */
a0d0e21e 1273 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
2fcb4757 1274 op_append_elem(OP_LIST, $3, scalar($2)));
f05e27e5
DM
1275 TOKEN_GETMAD($1,$$,'o');
1276 }
891be019 1277 | LOOPEX /* loop exiting command (goto, last, dump, etc) */
f05e27e5
DM
1278 { $$ = newOP(IVAL($1), OPf_SPECIAL);
1279 PL_hints |= HINT_BLOCK_SCOPE;
1280 TOKEN_GETMAD($1,$$,'o');
1281 }
a0d0e21e 1282 | LOOPEX term
f05e27e5
DM
1283 { $$ = newLOOPEX(IVAL($1),$2);
1284 TOKEN_GETMAD($1,$$,'o');
1285 }
09b1cffe 1286 | NOTOP listexpr /* not $foo */
f05e27e5
DM
1287 { $$ = newUNOP(OP_NOT, 0, scalar($2));
1288 TOKEN_GETMAD($1,$$,'o');
1289 }
891be019 1290 | UNIOP /* Unary op, $_ implied */
f05e27e5
DM
1291 { $$ = newOP(IVAL($1), 0);
1292 TOKEN_GETMAD($1,$$,'o');
1293 }
1294 | UNIOP block /* eval { foo }* */
1295 { $$ = newUNOP(IVAL($1), 0, $2);
1296 TOKEN_GETMAD($1,$$,'o');
1297 }
891be019 1298 | UNIOP term /* Unary op */
f05e27e5
DM
1299 { $$ = newUNOP(IVAL($1), 0, $2);
1300 TOKEN_GETMAD($1,$$,'o');
1301 }
d2fdf8fd
RGS
1302 | REQUIRE /* require, $_ implied */
1303 { $$ = newOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0);
1304 TOKEN_GETMAD($1,$$,'o');
1305 }
1306 | REQUIRE term /* require Foo */
1307 { $$ = newUNOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0, $2);
1308 TOKEN_GETMAD($1,$$,'o');
1309 }
3cd0a11a
RGS
1310 | UNIOPSUB
1311 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
891be019 1312 | UNIOPSUB term /* Sub treated as unop */
4633a7c4 1313 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
2fcb4757 1314 op_append_elem(OP_LIST, $2, scalar($1))); }
891be019 1315 | FUNC0 /* Nullary operator */
f05e27e5
DM
1316 { $$ = newOP(IVAL($1), 0);
1317 TOKEN_GETMAD($1,$$,'o');
1318 }
ae986130 1319 | FUNC0 '(' ')'
f05e27e5
DM
1320 { $$ = newOP(IVAL($1), 0);
1321 TOKEN_GETMAD($1,$$,'o');
1322 TOKEN_GETMAD($2,$$,'(');
1323 TOKEN_GETMAD($3,$$,')');
1324 }
7eb971ee
FC
1325 | FUNC0OP /* Same as above, but op created in toke.c */
1326 { $$ = $1; }
1327 | FUNC0OP '(' ')'
1328 { $$ = $1;
1329 TOKEN_GETMAD($2,$$,'(');
1330 TOKEN_GETMAD($3,$$,')');
1331 }
891be019 1332 | FUNC0SUB /* Sub treated as nullop */
28757baa 1333 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
4633a7c4 1334 scalar($1)); }
891be019 1335 | FUNC1 '(' ')' /* not () */
f05e27e5
DM
1336 { $$ = (IVAL($1) == OP_NOT)
1337 ? newUNOP(IVAL($1), 0, newSVOP(OP_CONST, 0, newSViv(0)))
1338 : newOP(IVAL($1), OPf_SPECIAL);
1339
1340 TOKEN_GETMAD($1,$$,'o');
1341 TOKEN_GETMAD($2,$$,'(');
1342 TOKEN_GETMAD($3,$$,')');
1343 }
891be019 1344 | FUNC1 '(' expr ')' /* not($foo) */
f05e27e5
DM
1345 { $$ = newUNOP(IVAL($1), 0, $3);
1346 TOKEN_GETMAD($1,$$,'o');
1347 TOKEN_GETMAD($2,$$,'(');
1348 TOKEN_GETMAD($4,$$,')');
1349 }
d63c20f2
DM
1350 | PMFUNC /* m//, s///, qr//, tr/// */
1351 {
1352 if ( $1->op_type != OP_TRANS
1353 && $1->op_type != OP_TRANSR
1354 && (((PMOP*)$1)->op_pmflags & PMf_HAS_CV))
1355 {
1356 $<ival>$ = start_subparse(FALSE, CVf_ANON);
1357 SAVEFREESV(PL_compcv);
1358 } else
1359 $<ival>$ = 0;
1360 }
1361 '(' listexpr ')'
1362 { $$ = pmruntime($1, $4, 1, $<ival>2);
1363 TOKEN_GETMAD($3,$$,'(');
1364 TOKEN_GETMAD($5,$$,')');
f05e27e5 1365 }
79072805 1366 | WORD
378cc40b 1367 | listop
be25f609 1368 | YADAYADA
1369 {
1370 $$ = newLISTOP(OP_DIE, 0, newOP(OP_PUSHMARK, 0),
1371 newSVOP(OP_CONST, 0, newSVpvs("Unimplemented")));
950f2ea2 1372 TOKEN_GETMAD($1,$$,'X');
be25f609 1373 }
88e1f1a2 1374 | PLUGEXPR
8d063cd8
LW
1375 ;
1376
891be019 1377/* "my" declarations, with optional attributes */
09bef843 1378myattrterm: MY myterm myattrlist
f05e27e5
DM
1379 { $$ = my_attrs($2,$3);
1380 DO_MAD(
1381 token_getmad($1,$$,'d');
1382 append_madprops($3->op_madprop, $$, 'a');
1383 $3->op_madprop = 0;
04ff073f 1384 );
f05e27e5 1385 }
09bef843 1386 | MY myterm
f05e27e5
DM
1387 { $$ = localize($2,IVAL($1));
1388 TOKEN_GETMAD($1,$$,'d');
1389 }
09bef843
SB
1390 ;
1391
891be019 1392/* Things that can be "my"'d */
09bef843 1393myterm : '(' expr ')'
f05e27e5
DM
1394 { $$ = sawparens($2);
1395 TOKEN_GETMAD($1,$$,'(');
1396 TOKEN_GETMAD($3,$$,')');
1397 }
09bef843 1398 | '(' ')'
f05e27e5
DM
1399 { $$ = sawparens(newNULLLIST());
1400 TOKEN_GETMAD($1,$$,'(');
1401 TOKEN_GETMAD($2,$$,')');
1402 }
09bef843
SB
1403 | scalar %prec '('
1404 { $$ = $1; }
1405 | hsh %prec '('
1406 { $$ = $1; }
1407 | ary %prec '('
1408 { $$ = $1; }
1409 ;
1410
891be019 1411/* Basic list expressions */
09b1cffe 1412optlistexpr: /* NULL */ %prec PREC_LOW
bcabcc50 1413 { $$ = (OP*)NULL; }
09b1cffe 1414 | listexpr %prec PREC_LOW
a0d0e21e
LW
1415 { $$ = $1; }
1416 ;
1417
09b1cffe 1418optexpr: /* NULL */
bcabcc50 1419 { $$ = (OP*)NULL; }
79072805
LW
1420 | expr
1421 { $$ = $1; }
1422 ;
1423
891be019
SC
1424/* A little bit of trickery to make "for my $foo (@bar)" actually be
1425 lexical */
55497cff 1426my_scalar: scalar
12bd6ede 1427 { PL_parser->in_my = 0; $$ = my($1); }
55497cff
PP
1428 ;
1429
79072805 1430amper : '&' indirob
f05e27e5
DM
1431 { $$ = newCVREF(IVAL($1),$2);
1432 TOKEN_GETMAD($1,$$,'&');
1433 }
a687059c
LW
1434 ;
1435
79072805 1436scalar : '$' indirob
f05e27e5
DM
1437 { $$ = newSVREF($2);
1438 TOKEN_GETMAD($1,$$,'$');
1439 }
a687059c
LW
1440 ;
1441
79072805 1442ary : '@' indirob
f05e27e5 1443 { $$ = newAVREF($2);
429a2555 1444 if ($$) $$->op_private |= IVAL($1);
f05e27e5
DM
1445 TOKEN_GETMAD($1,$$,'@');
1446 }
79072805
LW
1447 ;
1448
1449hsh : '%' indirob
f05e27e5 1450 { $$ = newHVREF($2);
95a31aad 1451 if ($$) $$->op_private |= IVAL($1);
f05e27e5
DM
1452 TOKEN_GETMAD($1,$$,'%');
1453 }
79072805
LW
1454 ;
1455
1456arylen : DOLSHARP indirob
f05e27e5
DM
1457 { $$ = newAVREF($2);
1458 TOKEN_GETMAD($1,$$,'l');
1459 }
79072805
LW
1460 ;
1461
1462star : '*' indirob
f05e27e5
DM
1463 { $$ = newGVREF(0,$2);
1464 TOKEN_GETMAD($1,$$,'*');
1465 }
79072805
LW
1466 ;
1467
891be019 1468/* Indirect objects */
79072805
LW
1469indirob : WORD
1470 { $$ = scalar($1); }
fad39ff1 1471 | scalar %prec PREC_LOW
f05e27e5 1472 { $$ = scalar($1); }
79072805 1473 | block
3ad73efd 1474 { $$ = op_scope($1); }
79072805 1475
93a17b20
LW
1476 | PRIVATEREF
1477 { $$ = $1; }
8d063cd8 1478 ;