This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
fix pod markup in warnings.pm
[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 *
23 * The main job of of this grammar is to call the various newFOO()
24 * functions in op.c to build a syntax tree of OP structs.
61296642 25 * It relies on the lexer in toke.c to do the tokenizing.
29522234
DM
26 *
27 * Note: due to the way that the cleanup code works WRT to freeing ops on
28 * the parse stack, it is dangerous to assign to the $n variables within
29 * an action.
166f8a29
DM
30 */
31
0de566d7 32/* Make the parser re-entrant. */
8d063cd8 33
f39ff1f3 34%pure-parser
8d063cd8 35
28ac2b49 36%start grammar
9d116dd7 37
8d063cd8 38%union {
d5c6462e
DM
39 I32 ival; /* __DEFAULT__ (marker for regen_perly.pl;
40 must always be 1st union member) */
79072805
LW
41 char *pval;
42 OP *opval;
43 GV *gvval;
8d063cd8
LW
44}
45
78cdf107 46%token <ival> GRAMPROG GRAMEXPR GRAMBLOCK GRAMBARESTMT GRAMFULLSTMT GRAMSTMTSEQ
28ac2b49 47
f10ebf6c 48%token <ival> '{' '}' '[' ']' '-' '+' '@' '%' '&' '=' '.'
f0fcb552 49
185c2e96 50%token <opval> BAREWORD METHOD FUNCMETH THING PMFUNC PRIVATEREF QWLIST
7eb971ee 51%token <opval> FUNC0OP FUNC0SUB UNIOPSUB LSTOPSUB
88e1f1a2 52%token <opval> PLUGEXPR PLUGSTMT
b5bbe64a
JH
53%token <pval> LABEL
54%token <ival> FORMAT SUB ANONSUB PACKAGE USE
55%token <ival> WHILE UNTIL IF UNLESS ELSE ELSIF CONTINUE FOR
284a3526 56%token <ival> GIVEN WHERESO
b5bbe64a
JH
57%token <ival> LOOPEX DOTDOT YADAYADA
58%token <ival> FUNC0 FUNC1 FUNC UNIOP LSTOP
59%token <ival> RELOP EQOP MULOP ADDOP
60%token <ival> DOLSHARP DO HASHBRACK NOAMP
61%token <ival> LOCAL MY REQUIRE
62%token <ival> COLONATTR FORMLBRACK FORMRBRACK
f05e27e5 63
727a8fe5 64%type <ival> grammar remember mremember
f05e27e5 65%type <ival> startsub startanonsub startformsub
b5bbe64a 66
b5a64814 67%type <ival> mintro
f05e27e5 68
8e720305 69%type <opval> stmtseq fullstmt labfullstmt barestmt block mblock else
fad39ff1 70%type <opval> expr term subscripted scalar ary hsh arylen star amper sideff
76eba8ab 71%type <opval> sliceme kvslice gelem
08b3e84f 72%type <opval> listexpr nexpr texpr iexpr mexpr mnexpr
9b6b7be8 73%type <opval> optlistexpr optexpr optrepl indirob listop method
d39c26a6
FC
74%type <opval> formname subname proto optsubbody cont my_scalar my_var
75%type <opval> refgen_topic formblock
f05e27e5 76%type <opval> subattrlist myattrlist myattrterm myterm
d3d9da4a
DM
77%type <opval> termbinop termunop anonymous termdo
78%type <ival> sigslurpsigil
79%type <opval> sigvarname sigdefault sigscalarelem sigslurpelem
80%type <opval> sigelem siglist siglistornull subsignature
705fe0e5 81%type <opval> formstmtseq formline formarg
79072805 82
b5bbe64a 83%nonassoc <ival> PREC_LOW
fad39ff1
SM
84%nonassoc LOOPEX
85
b5bbe64a
JH
86%left <ival> OROP DOROP
87%left <ival> ANDOP
88%right <ival> NOTOP
36477c24 89%nonassoc LSTOP LSTOPSUB
b5bbe64a
JH
90%left <ival> ','
91%right <ival> ASSIGNOP
92%right <ival> '?' ':'
29d69c3c 93%nonassoc DOTDOT
b5bbe64a
JH
94%left <ival> OROR DORDOR
95%left <ival> ANDAND
96%left <ival> BITOROP
97%left <ival> BITANDOP
a687059c
LW
98%nonassoc EQOP
99%nonassoc RELOP
36477c24 100%nonassoc UNIOP UNIOPSUB
20515881 101%nonassoc REQUIRE
b5bbe64a 102%left <ival> SHIFTOP
a687059c
LW
103%left ADDOP
104%left MULOP
b5bbe64a
JH
105%left <ival> MATCHOP
106%right <ival> '!' '~' UMINUS REFGEN
107%right <ival> POWOP
108%nonassoc <ival> PREINC PREDEC POSTINC POSTDEC POSTJOIN
109%left <ival> ARROW
110%nonassoc <ival> ')'
111%left <ival> '('
fad39ff1 112%left '[' '{'
8d063cd8
LW
113
114%% /* RULES */
115
28ac2b49 116/* Top-level choice of what kind of thing yyparse was called to parse */
727a8fe5
Z
117grammar : GRAMPROG
118 {
624fa8bd 119 parser->expect = XSTATE;
5a2060e2 120 $<ival>$ = 0;
727a8fe5
Z
121 }
122 remember stmtseq
123 {
124 newPROG(block_end($3,$4));
8635e3c2 125 PL_compiling.cop_seq = 0;
727a8fe5
Z
126 $$ = 0;
127 }
78cdf107
Z
128 | GRAMEXPR
129 {
130 parser->expect = XTERM;
5a2060e2 131 $<ival>$ = 0;
78cdf107 132 }
09b1cffe 133 optexpr
78cdf107
Z
134 {
135 PL_eval_root = $3;
136 $$ = 0;
137 }
e53d8f76
Z
138 | GRAMBLOCK
139 {
140 parser->expect = XBLOCK;
5a2060e2 141 $<ival>$ = 0;
e53d8f76
Z
142 }
143 block
144 {
5f211341 145 PL_pad_reset_pending = TRUE;
8359b381
Z
146 PL_eval_root = $3;
147 $$ = 0;
148 yyunlex();
0f8490d1 149 parser->yychar = yytoken = YYEOF;
8359b381
Z
150 }
151 | GRAMBARESTMT
152 {
153 parser->expect = XSTATE;
5a2060e2 154 $<ival>$ = 0;
8359b381
Z
155 }
156 barestmt
157 {
158 PL_pad_reset_pending = TRUE;
e53d8f76
Z
159 PL_eval_root = $3;
160 $$ = 0;
161 yyunlex();
0f8490d1 162 parser->yychar = yytoken = YYEOF;
e53d8f76 163 }
9eb5c532 164 | GRAMFULLSTMT
28ac2b49 165 {
9eb5c532 166 parser->expect = XSTATE;
5a2060e2 167 $<ival>$ = 0;
9eb5c532
Z
168 }
169 fullstmt
170 {
5f211341 171 PL_pad_reset_pending = TRUE;
9eb5c532 172 PL_eval_root = $3;
28ac2b49
Z
173 $$ = 0;
174 yyunlex();
0f8490d1 175 parser->yychar = yytoken = YYEOF;
28ac2b49 176 }
07ffcb73
Z
177 | GRAMSTMTSEQ
178 {
179 parser->expect = XSTATE;
5a2060e2 180 $<ival>$ = 0;
07ffcb73 181 }
5f211341 182 stmtseq
07ffcb73
Z
183 {
184 PL_eval_root = $3;
185 $$ = 0;
186 }
187 ;
188
891be019 189/* An ordinary block */
5f211341 190block : '{' remember stmtseq '}'
624fa8bd
FC
191 { if (parser->copline > (line_t)$1)
192 parser->copline = (line_t)$1;
f05e27e5 193 $$ = block_end($2, $3);
f05e27e5 194 }
a0d0e21e
LW
195 ;
196
7c70caa5 197/* format body */
705fe0e5 198formblock: '=' remember ';' FORMRBRACK formstmtseq ';' '.'
624fa8bd
FC
199 { if (parser->copline > (line_t)$1)
200 parser->copline = (line_t)$1;
705fe0e5 201 $$ = block_end($2, $5);
7c70caa5
FC
202 }
203 ;
204
55497cff 205remember: /* NULL */ /* start a full lexical scope */
34b54951
FC
206 { $$ = block_start(TRUE);
207 parser->parsed_sub = 0; }
55497cff 208 ;
209
5f211341 210mblock : '{' mremember stmtseq '}'
624fa8bd
FC
211 { if (parser->copline > (line_t)$1)
212 parser->copline = (line_t)$1;
f05e27e5 213 $$ = block_end($2, $3);
f05e27e5 214 }
55497cff 215 ;
216
217mremember: /* NULL */ /* start a partial lexical scope */
34b54951
FC
218 { $$ = block_start(FALSE);
219 parser->parsed_sub = 0; }
8d063cd8
LW
220 ;
221
eae48c89 222/* A sequence of statements in the program */
5f211341 223stmtseq : /* NULL */
a9f5ab8d 224 { $$ = NULL; }
5f211341 225 | stmtseq fullstmt
2fcb4757 226 { $$ = op_append_list(OP_LINESEQ, $1, $2);
3280af22 227 PL_pad_reset_pending = TRUE;
503de470
DM
228 if ($1 && $2)
229 PL_hints |= HINT_BLOCK_SCOPE;
230 }
8d063cd8
LW
231 ;
232
705fe0e5
FC
233/* A sequence of format lines */
234formstmtseq: /* NULL */
a9f5ab8d 235 { $$ = NULL; }
705fe0e5
FC
236 | formstmtseq formline
237 { $$ = op_append_list(OP_LINESEQ, $1, $2);
238 PL_pad_reset_pending = TRUE;
239 if ($1 && $2)
240 PL_hints |= HINT_BLOCK_SCOPE;
241 }
242 ;
243
8e720305
Z
244/* A statement in the program, including optional labels */
245fullstmt: barestmt
eae48c89 246 {
b5bbe64a 247 $$ = $1 ? newSTATEOP(0, NULL, $1) : NULL;
eae48c89 248 }
8e720305
Z
249 | labfullstmt
250 { $$ = $1; }
251 ;
252
253labfullstmt: LABEL barestmt
254 {
b5bbe64a 255 $$ = newSTATEOP(SVf_UTF8 * $1[strlen($1)+1], $1, $2);
8e720305
Z
256 }
257 | LABEL labfullstmt
258 {
b5bbe64a 259 $$ = newSTATEOP(SVf_UTF8 * $1[strlen($1)+1], $1, $2);
8e720305 260 }
eae48c89
Z
261 ;
262
263/* A bare statement, lacking label and other aspects of state op */
264barestmt: PLUGSTMT
0d863452 265 { $$ = $1; }
7c70caa5 266 | FORMAT startformsub formname formblock
f05e27e5 267 {
eae48c89 268 CV *fmtcv = PL_compcv;
eae48c89 269 newFORM($2, $3, $4);
a9f5ab8d 270 $$ = NULL;
4a273b91 271 if (CvOUTSIDE(fmtcv) && !CvEVAL(CvOUTSIDE(fmtcv))) {
74a9453a 272 pad_add_weakref(fmtcv);
f05e27e5 273 }
8b9fb2f9 274 parser->parsed_sub = 1;
eae48c89 275 }
50278755
FC
276 | SUB subname startsub
277 {
278 if ($2->op_type == OP_CONST) {
279 const char *const name =
280 SvPV_nolen_const(((SVOP*)$2)->op_sv);
281 if (strEQ(name, "BEGIN") || strEQ(name, "END")
764212cf
FC
282 || strEQ(name, "INIT") || strEQ(name, "CHECK")
283 || strEQ(name, "UNITCHECK"))
284 CvSPECIAL_on(PL_compcv);
50278755 285 }
e07561e6
FC
286 else
287 /* State subs inside anonymous subs need to be
288 clonable themselves. */
6d5c2147
FC
289 if (CvANON(CvOUTSIDE(PL_compcv))
290 || CvCLONE(CvOUTSIDE(PL_compcv))
291 || !PadnameIsSTATE(PadlistNAMESARRAY(CvPADLIST(
292 CvOUTSIDE(PL_compcv)
293 ))[$2->op_targ]))
e07561e6 294 CvCLONE_on(PL_compcv);
624fa8bd
FC
295 parser->in_my = 0;
296 parser->in_my_stash = NULL;
764212cf 297 }
30d9c59b 298 proto subattrlist optsubbody
eae48c89
Z
299 {
300 SvREFCNT_inc_simple_void(PL_compcv);
50278755
FC
301 $2->op_type == OP_CONST
302 ? newATTRSUB($3, $2, $5, $6, $7)
303 : newMYSUB($3, $2, $5, $6, $7)
50278755 304 ;
a9f5ab8d 305 $$ = NULL;
764212cf 306 intro_my();
34b54951 307 parser->parsed_sub = 1;
f05e27e5 308 }
abcf453d
PM
309 | SUB subname startsub
310 {
311 if ($2->op_type == OP_CONST) {
312 const char *const name =
313 SvPV_nolen_const(((SVOP*)$2)->op_sv);
314 if (strEQ(name, "BEGIN") || strEQ(name, "END")
315 || strEQ(name, "INIT") || strEQ(name, "CHECK")
316 || strEQ(name, "UNITCHECK"))
317 CvSPECIAL_on(PL_compcv);
318 }
319 else
320 /* State subs inside anonymous subs need to be
321 clonable themselves. */
322 if (CvANON(CvOUTSIDE(PL_compcv))
323 || CvCLONE(CvOUTSIDE(PL_compcv))
324 || !PadnameIsSTATE(PadlistNAMESARRAY(CvPADLIST(
325 CvOUTSIDE(PL_compcv)
326 ))[$2->op_targ]))
327 CvCLONE_on(PL_compcv);
328 parser->in_my = 0;
329 parser->in_my_stash = NULL;
330 }
331 remember subsignature subattrlist '{' stmtseq '}'
332 {
333 OP *body;
334 if (parser->copline > (line_t)$8)
335 parser->copline = (line_t)$8;
336 body = block_end($5,
337 op_append_list(OP_LINESEQ, $6, $9));
338
339 SvREFCNT_inc_simple_void(PL_compcv);
340 $2->op_type == OP_CONST
341 ? newATTRSUB($3, $2, NULL, $7, body)
342 : newMYSUB($3, $2, NULL, $7, body)
343 ;
a9f5ab8d 344 $$ = NULL;
abcf453d
PM
345 intro_my();
346 parser->parsed_sub = 1;
347 }
185c2e96 348 | PACKAGE BAREWORD BAREWORD ';'
5f211341 349 {
eae48c89
Z
350 package($3);
351 if ($2)
352 package_version($2);
a9f5ab8d 353 $$ = NULL;
eae48c89
Z
354 }
355 | USE startsub
356 { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
185c2e96 357 BAREWORD BAREWORD optlistexpr ';'
eae48c89
Z
358 {
359 SvREFCNT_inc_simple_void(PL_compcv);
b5bbe64a 360 utilize($1, $2, $4, $5, $6);
34b54951 361 parser->parsed_sub = 1;
a9f5ab8d 362 $$ = NULL;
eae48c89 363 }
417a992d 364 | IF '(' remember mexpr ')' mblock else
eae48c89 365 {
3ad73efd
Z
366 $$ = block_end($3,
367 newCONDOP(0, $4, op_scope($6), $7));
624fa8bd 368 parser->copline = (line_t)$1;
eae48c89 369 }
08b3e84f 370 | UNLESS '(' remember mexpr ')' mblock else
eae48c89 371 {
3ad73efd 372 $$ = block_end($3,
08b3e84f 373 newCONDOP(0, $4, $7, op_scope($6)));
624fa8bd 374 parser->copline = (line_t)$1;
eae48c89 375 }
b5a64814 376 | GIVEN '(' remember mexpr ')' mblock
eae48c89 377 {
5d051ee0 378 $$ = block_end($3, newGIVENOP($4, op_scope($6), 0));
624fa8bd 379 parser->copline = (line_t)$1;
eae48c89 380 }
284a3526 381 | WHERESO '(' remember mexpr ')' mblock
f68519ee
Z
382 {
383 OP *cond = $4;
384 if ($1)
385 cond = newBINOP(OP_SMARTMATCH, 0, newDEFSVOP(),
386 scalar(cond));
387 $$ = block_end($3, newWHERESOOP(cond, op_scope($6)));
388 }
417a992d 389 | WHILE '(' remember texpr ')' mintro mblock cont
eae48c89
Z
390 {
391 $$ = block_end($3,
a9f5ab8d 392 newWHILEOP(0, 1, NULL,
94bf0465 393 $4, $7, $8, $6));
624fa8bd 394 parser->copline = (line_t)$1;
eae48c89 395 }
417a992d 396 | UNTIL '(' remember iexpr ')' mintro mblock cont
eae48c89
Z
397 {
398 $$ = block_end($3,
a9f5ab8d 399 newWHILEOP(0, 1, NULL,
94bf0465 400 $4, $7, $8, $6));
624fa8bd 401 parser->copline = (line_t)$1;
eae48c89 402 }
2d0e3c96
FC
403 | FOR '(' remember mnexpr ';'
404 { parser->expect = XTERM; }
405 texpr ';'
406 { parser->expect = XTERM; }
407 mintro mnexpr ')'
eae48c89
Z
408 mblock
409 {
b5bbe64a 410 OP *initop = $4;
a9f5ab8d 411 OP *forop = newWHILEOP(0, 1, NULL,
2d0e3c96 412 scalar($7), $13, $11, $10);
eae48c89
Z
413 if (initop) {
414 forop = op_prepend_elem(OP_LINESEQ, initop,
415 op_append_elem(OP_LINESEQ,
416 newOP(OP_UNSTACK, OPf_SPECIAL),
417 forop));
5f211341 418 }
0f602692 419 PL_hints |= HINT_BLOCK_SCOPE;
eae48c89 420 $$ = block_end($3, forop);
624fa8bd 421 parser->copline = (line_t)$1;
eae48c89 422 }
417a992d 423 | FOR MY remember my_scalar '(' mexpr ')' mblock cont
eae48c89 424 {
94bf0465 425 $$ = block_end($3, newFOROP(0, $4, $6, $8, $9));
624fa8bd 426 parser->copline = (line_t)$1;
eae48c89 427 }
417a992d 428 | FOR scalar '(' remember mexpr ')' mblock cont
eae48c89 429 {
94bf0465 430 $$ = block_end($4, newFOROP(0,
3ad73efd 431 op_lvalue($2, OP_ENTERLOOP), $5, $7, $8));
624fa8bd 432 parser->copline = (line_t)$1;
eae48c89 433 }
e118fea3
FC
434 | FOR my_refgen remember my_var
435 { parser->in_my = 0; $<opval>$ = my($4); }
d39c26a6
FC
436 '(' mexpr ')' mblock cont
437 {
438 $$ = block_end(
e118fea3 439 $3,
d39c26a6
FC
440 newFOROP(0,
441 op_lvalue(
442 newUNOP(OP_REFGEN, 0,
e118fea3 443 $<opval>5),
d39c26a6 444 OP_ENTERLOOP),
e118fea3 445 $7, $9, $10)
d39c26a6
FC
446 );
447 parser->copline = (line_t)$1;
448 }
449 | FOR REFGEN refgen_topic '(' remember mexpr ')' mblock cont
450 {
451 $$ = block_end($5, newFOROP(
452 0, op_lvalue(newUNOP(OP_REFGEN, 0,
96801525 453 $3),
d39c26a6
FC
454 OP_ENTERLOOP), $6, $8, $9));
455 parser->copline = (line_t)$1;
456 }
417a992d 457 | FOR '(' remember mexpr ')' mblock cont
eae48c89
Z
458 {
459 $$ = block_end($3,
a9f5ab8d 460 newFOROP(0, NULL, $4, $6, $7));
624fa8bd 461 parser->copline = (line_t)$1;
eae48c89
Z
462 }
463 | block cont
464 {
465 /* a block is a loop that happens once */
a9f5ab8d
LM
466 $$ = newWHILEOP(0, 1, NULL,
467 NULL, $1, $2, 0);
eae48c89 468 }
185c2e96 469 | PACKAGE BAREWORD BAREWORD '{' remember
eae48c89 470 {
eae48c89 471 package($3);
eae48c89 472 if ($2) {
eae48c89 473 package_version($2);
eae48c89
Z
474 }
475 }
476 stmtseq '}'
477 {
478 /* a block is a loop that happens once */
a9f5ab8d
LM
479 $$ = newWHILEOP(0, 1, NULL,
480 NULL, block_end($5, $7), NULL, 0);
624fa8bd
FC
481 if (parser->copline > (line_t)$4)
482 parser->copline = (line_t)$4;
eae48c89
Z
483 }
484 | sideff ';'
485 {
eae48c89 486 $$ = $1;
eae48c89 487 }
29d69c3c
Z
488 | YADAYADA ';'
489 {
490 $$ = newLISTOP(OP_DIE, 0, newOP(OP_PUSHMARK, 0),
491 newSVOP(OP_CONST, 0, newSVpvs("Unimplemented")));
492 }
eae48c89
Z
493 | ';'
494 {
a9f5ab8d 495 $$ = NULL;
624fa8bd 496 parser->copline = NOLINE;
5f211341 497 }
8d063cd8
LW
498 ;
499
705fe0e5
FC
500/* Format line */
501formline: THING formarg
502 { OP *list;
503 if ($2) {
504 OP *term = $2;
705fe0e5
FC
505 list = op_append_elem(OP_LIST, $1, term);
506 }
507 else {
705fe0e5 508 list = $1;
705fe0e5 509 }
624fa8bd
FC
510 if (parser->copline == NOLINE)
511 parser->copline = CopLINE(PL_curcop)-1;
512 else parser->copline--;
705fe0e5 513 $$ = newSTATEOP(0, NULL,
03d05f6e 514 op_convert_list(OP_FORMLINE, 0, list));
705fe0e5
FC
515 }
516 ;
517
518formarg : /* NULL */
519 { $$ = NULL; }
520 | FORMLBRACK stmtseq FORMRBRACK
521 { $$ = op_unscope($2); }
522 ;
523
891be019 524/* An expression which may have a side-effect */
a687059c 525sideff : error
a9f5ab8d 526 { $$ = NULL; }
a687059c 527 | expr
79072805 528 { $$ = $1; }
a687059c 529 | expr IF expr
b5bbe64a 530 { $$ = newLOGOP(OP_AND, 0, $3, $1); }
a687059c 531 | expr UNLESS expr
b5bbe64a 532 { $$ = newLOGOP(OP_OR, 0, $3, $1); }
a687059c 533 | expr WHILE expr
b5bbe64a 534 { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1); }
55497cff 535 | expr UNTIL iexpr
b5bbe64a 536 { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1); }
ecca16b0 537 | expr FOR expr
a9f5ab8d 538 { $$ = newFOROP(0, NULL, $3, $1, NULL);
624fa8bd 539 parser->copline = (line_t)$2; }
284a3526 540 | expr WHERESO expr
f68519ee
Z
541 {
542 OP *cond = $3;
543 if ($2)
544 cond = newBINOP(OP_SMARTMATCH, 0, newDEFSVOP(),
545 scalar(cond));
546 $$ = newWHERESOOP(cond, op_scope($1));
547 }
79072805
LW
548 ;
549
891be019 550/* else and elsif blocks */
79072805 551else : /* NULL */
a9f5ab8d 552 { $$ = NULL; }
55497cff 553 | ELSE mblock
3ad73efd
Z
554 {
555 ($2)->op_flags |= OPf_PARENS;
556 $$ = op_scope($2);
f05e27e5 557 }
417a992d 558 | ELSIF '(' mexpr ')' mblock else
624fa8bd 559 { parser->copline = (line_t)$1;
3ad73efd
Z
560 $$ = newCONDOP(0,
561 newSTATEOP(OPf_SPECIAL,NULL,$3),
562 op_scope($5), $6);
563 PL_hints |= HINT_BLOCK_SCOPE;
f05e27e5 564 }
79072805
LW
565 ;
566
891be019 567/* Continue blocks */
79072805 568cont : /* NULL */
a9f5ab8d 569 { $$ = NULL; }
79072805 570 | CONTINUE block
b5bbe64a 571 { $$ = op_scope($2); }
79072805
LW
572 ;
573
a034e688
DM
574/* determine whether there are any new my declarations */
575mintro : /* NULL */
576 { $$ = (PL_min_intro_pending &&
577 PL_max_intro_pending >= PL_min_intro_pending);
578 intro_my(); }
579
891be019 580/* Normal expression */
8d063cd8 581nexpr : /* NULL */
a9f5ab8d 582 { $$ = NULL; }
8d063cd8
LW
583 | sideff
584 ;
585
891be019 586/* Boolean expression */
8d063cd8 587texpr : /* NULL means true */
f05e27e5
DM
588 { YYSTYPE tmplval;
589 (void)scan_num("1", &tmplval);
590 $$ = tmplval.opval; }
8d063cd8
LW
591 | expr
592 ;
593
891be019 594/* Inverted boolean expression */
55497cff 595iexpr : expr
596 { $$ = invert(scalar($1)); }
597 ;
598
891be019 599/* Expression with its own lexical scope */
55497cff 600mexpr : expr
bbce6d69 601 { $$ = $1; intro_my(); }
602 ;
603
604mnexpr : nexpr
605 { $$ = $1; intro_my(); }
55497cff 606 ;
607
185c2e96 608formname: BAREWORD { $$ = $1; }
a9f5ab8d 609 | /* NULL */ { $$ = NULL; }
8d063cd8
LW
610 ;
611
fa83b5b6 612startsub: /* NULL */ /* start a regular subroutine scope */
a8ff2fa6
DM
613 { $$ = start_subparse(FALSE, 0);
614 SAVEFREESV(PL_compcv); }
f05e27e5 615
28757baa 616 ;
617
618startanonsub: /* NULL */ /* start an anonymous subroutine scope */
a8ff2fa6
DM
619 { $$ = start_subparse(FALSE, CVf_ANON);
620 SAVEFREESV(PL_compcv); }
28757baa 621 ;
622
44a8e56a 623startformsub: /* NULL */ /* start a format subroutine scope */
a8ff2fa6
DM
624 { $$ = start_subparse(TRUE, 0);
625 SAVEFREESV(PL_compcv); }
44a8e56a 626 ;
627
891be019 628/* Name of a subroutine - must be a bareword, could be special */
185c2e96 629subname : BAREWORD
4210d3f1 630 | PRIVATEREF
a0d0e21e
LW
631 ;
632
891be019 633/* Subroutine prototype */
4633a7c4 634proto : /* NULL */
a9f5ab8d 635 { $$ = NULL; }
4633a7c4
LW
636 | THING
637 ;
28757baa 638
891be019 639/* Optional list of subroutine attributes */
09bef843 640subattrlist: /* NULL */
a9f5ab8d 641 { $$ = NULL; }
09bef843 642 | COLONATTR THING
b5bbe64a 643 { $$ = $2; }
09bef843 644 | COLONATTR
a9f5ab8d 645 { $$ = NULL; }
09bef843
SB
646 ;
647
891be019 648/* List of attributes for a "my" variable declaration */
09bef843 649myattrlist: COLONATTR THING
b5bbe64a 650 { $$ = $2; }
09bef843 651 | COLONATTR
a9f5ab8d 652 { $$ = NULL; }
09bef843
SB
653 ;
654
abcf453d 655
d3d9da4a
DM
656
657/* --------------------------------------
658 * subroutine signature parsing
659 */
660
661/* the '' or 'foo' part of a '$' or '@foo' etc signature variable */
49fb8620 662sigvarname: /* NULL */
a9f5ab8d 663 { parser->in_my = 0; $$ = NULL; }
d3d9da4a 664 | PRIVATEREF
49fb8620 665 { parser->in_my = 0; $$ = $1; }
d3d9da4a
DM
666 ;
667
668sigslurpsigil:
669 '@'
670 { $$ = '@'; }
671 | '%'
672 { $$ = '%'; }
673
674/* @, %, @foo, %foo */
675sigslurpelem: sigslurpsigil sigvarname sigdefault/* def only to catch errors */
676 {
4fa06845
DM
677 I32 sigil = $1;
678 OP *var = $2;
d3d9da4a 679 OP *defexpr = $3;
d3d9da4a 680
036bbc13 681 if (parser->sig_slurpy)
d3d9da4a 682 yyerror("Multiple slurpy parameters not allowed");
036bbc13 683 parser->sig_slurpy = (char)sigil;
d3d9da4a
DM
684
685 if (defexpr)
bb6b75cd 686 yyerror("A slurpy parameter may not have "
d3d9da4a
DM
687 "a default value");
688
a9f5ab8d 689 $$ = var ? newSTATEOP(0, NULL, var) : NULL;
d3d9da4a
DM
690 }
691 ;
692
693/* default part of sub signature scalar element: i.e. '= default_expr' */
694sigdefault: /* NULL */
a9f5ab8d 695 { $$ = NULL; }
d3d9da4a
DM
696 | ASSIGNOP
697 { $$ = newOP(OP_NULL, 0); }
698 | ASSIGNOP term
699 { $$ = $2; }
700
701
702/* subroutine signature scalar element: e.g. '$x', '$=', '$x = $default' */
703sigscalarelem:
704 '$' sigvarname sigdefault
705 {
706 OP *var = $2;
707 OP *defexpr = $3;
d3d9da4a 708
036bbc13 709 if (parser->sig_slurpy)
d3d9da4a
DM
710 yyerror("Slurpy parameter not last");
711
036bbc13 712 parser->sig_elems++;
d3d9da4a 713
d3d9da4a 714 if (defexpr) {
036bbc13 715 parser->sig_optelems++;
4fa06845
DM
716
717 if ( defexpr->op_type == OP_NULL
d3d9da4a
DM
718 && !(defexpr->op_flags & OPf_KIDS))
719 {
4fa06845
DM
720 /* handle '$=' special case */
721 if (var)
722 yyerror("Optional parameter "
723 "lacks default expression");
d3d9da4a
DM
724 op_free(defexpr);
725 }
4fa06845
DM
726 else {
727 /* a normal '=default' expression */
728 OP *defop = (OP*)alloc_LOGOP(OP_ARGDEFELEM,
729 defexpr,
730 LINKLIST(defexpr));
731 /* re-purpose op_targ to hold @_ index */
6daeaaa3 732 defop->op_targ =
036bbc13 733 (PADOFFSET)(parser->sig_elems - 1);
4fa06845
DM
734
735 if (var) {
736 var->op_flags |= OPf_STACKED;
737 (void)op_sibling_splice(var,
738 NULL, 0, defop);
739 scalar(defop);
740 }
741 else
742 var = newUNOP(OP_NULL, 0, defop);
743
744 LINKLIST(var);
745 /* NB: normally the first child of a
746 * logop is executed before the logop,
747 * and it pushes a boolean result
748 * ready for the logop. For ARGDEFELEM,
749 * the op itself does the boolean
750 * calculation, so set the first op to
751 * it instead.
752 */
753 var->op_next = defop;
754 defexpr->op_next = var;
d3d9da4a
DM
755 }
756 }
757 else {
036bbc13 758 if (parser->sig_optelems)
d3d9da4a
DM
759 yyerror("Mandatory parameter "
760 "follows optional parameter");
d3d9da4a
DM
761 }
762
a9f5ab8d 763 $$ = var ? newSTATEOP(0, NULL, var) : NULL;
d3d9da4a
DM
764 }
765 ;
766
767
768/* subroutine signature element: e.g. '$x = $default' or '%h' */
769sigelem: sigscalarelem
49fb8620 770 { parser->in_my = KEY_sigvar; $$ = $1; }
d3d9da4a 771 | sigslurpelem
49fb8620 772 { parser->in_my = KEY_sigvar; $$ = $1; }
d3d9da4a
DM
773 ;
774
775/* list of subroutine signature elements */
776siglist:
777 siglist ','
778 { $$ = $1; }
779 | siglist ',' sigelem
30d9c59b 780 {
d3d9da4a 781 $$ = op_append_list(OP_LINESEQ, $1, $3);
30d9c59b 782 }
d3d9da4a
DM
783 | sigelem %prec PREC_LOW
784 { $$ = $1; }
30d9c59b
Z
785 ;
786
d3d9da4a
DM
787/* () or (....) */
788siglistornull: /* NULL */
a9f5ab8d 789 { $$ = NULL; }
d3d9da4a
DM
790 | siglist
791 { $$ = $1; }
792
793/* Subroutine signature */
794subsignature: '('
795 {
796 ENTER;
036bbc13
FC
797 SAVEIV(parser->sig_elems);
798 SAVEIV(parser->sig_optelems);
799 SAVEI8(parser->sig_slurpy);
800 parser->sig_elems = 0;
801 parser->sig_optelems = 0;
802 parser->sig_slurpy = 0;
49fb8620 803 parser->in_my = KEY_sigvar;
d3d9da4a
DM
804 }
805 siglistornull
806 ')'
807 {
4fa06845
DM
808 OP *sigops = $3;
809 UNOP_AUX_item *aux;
810 OP *check;
d3d9da4a 811
bdc377e5
FC
812 if (!parser->error_count) {
813 assert(FEATURE_SIGNATURES_IS_ENABLED);
814 }
d3d9da4a
DM
815
816 /* We shouldn't get here otherwise */
817 Perl_ck_warner_d(aTHX_
818 packWARN(WARN_EXPERIMENTAL__SIGNATURES),
819 "The signatures feature is experimental");
820
4fa06845
DM
821 aux = (UNOP_AUX_item*)PerlMemShared_malloc(
822 sizeof(UNOP_AUX_item) * 3);
036bbc13
FC
823 aux[0].iv = parser->sig_elems;
824 aux[1].iv = parser->sig_optelems;
825 aux[2].iv = parser->sig_slurpy;
4fa06845
DM
826 check = newUNOP_AUX(OP_ARGCHECK, 0, NULL, aux);
827 sigops = op_prepend_elem(OP_LINESEQ, check, sigops);
828 sigops = op_prepend_elem(OP_LINESEQ,
829 newSTATEOP(0, NULL, NULL),
830 sigops);
831 /* a nextstate at the end handles context
832 * correctly for an empty sub body */
833 $$ = op_append_elem(OP_LINESEQ,
834 sigops,
835 newSTATEOP(0, NULL, NULL));
d3d9da4a 836
49fb8620 837 parser->in_my = 0;
d3d9da4a
DM
838 parser->expect = XATTRBLOCK;
839 LEAVE;
840 }
841 ;
842
843
844
30d9c59b 845/* Optional subroutine body, for named subroutine declaration */
abcf453d 846optsubbody: block
a9f5ab8d 847 | ';' { $$ = NULL; }
8d063cd8
LW
848 ;
849
891be019 850/* Ordinary expressions; logical combinations */
a0d0e21e 851expr : expr ANDOP expr
b5bbe64a 852 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
a0d0e21e 853 | expr OROP expr
b5bbe64a 854 { $$ = newLOGOP($2, 0, $1, $3); }
c963b151 855 | expr DOROP expr
b5bbe64a 856 { $$ = newLOGOP(OP_DOR, 0, $1, $3); }
09b1cffe 857 | listexpr %prec PREC_LOW
a0d0e21e
LW
858 ;
859
891be019 860/* Expressions are a list of terms joined by commas */
09b1cffe 861listexpr: listexpr ','
b5bbe64a 862 { $$ = $1; }
09b1cffe 863 | listexpr ',' term
abcf453d 864 {
29522234 865 OP* term = $3;
2fcb4757 866 $$ = op_append_elem(OP_LIST, $1, term);
f05e27e5 867 }
fad39ff1 868 | term %prec PREC_LOW
8d063cd8
LW
869 ;
870
891be019 871/* List operators */
09b1cffe 872listop : LSTOP indirob listexpr /* map {...} @args or print $fh @args */
03d05f6e 873 { $$ = op_convert_list($1, OPf_STACKED,
b5bbe64a 874 op_prepend_elem(OP_LIST, newGVREF($1,$2), $3) );
f05e27e5 875 }
891be019 876 | FUNC '(' indirob expr ')' /* print ($fh @args */
03d05f6e 877 { $$ = op_convert_list($1, OPf_STACKED,
b5bbe64a 878 op_prepend_elem(OP_LIST, newGVREF($1,$3), $4) );
f05e27e5 879 }
417a992d 880 | term ARROW method '(' optexpr ')' /* $foo->bar(list) */
03d05f6e 881 { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
2fcb4757
Z
882 op_append_elem(OP_LIST,
883 op_prepend_elem(OP_LIST, scalar($1), $5),
b46e009d 884 newMETHOP(OP_METHOD, 0, $3)));
f05e27e5 885 }
891be019 886 | term ARROW method /* $foo->bar */
03d05f6e 887 { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
2fcb4757 888 op_append_elem(OP_LIST, scalar($1),
b46e009d 889 newMETHOP(OP_METHOD, 0, $3)));
f05e27e5 890 }
09b1cffe 891 | METHOD indirob optlistexpr /* new Class @args */
03d05f6e 892 { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
2fcb4757
Z
893 op_append_elem(OP_LIST,
894 op_prepend_elem(OP_LIST, $2, $3),
b46e009d 895 newMETHOP(OP_METHOD, 0, $1)));
f05e27e5 896 }
09b1cffe 897 | FUNCMETH indirob '(' optexpr ')' /* method $object (@args) */
03d05f6e 898 { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
2fcb4757
Z
899 op_append_elem(OP_LIST,
900 op_prepend_elem(OP_LIST, $2, $4),
b46e009d 901 newMETHOP(OP_METHOD, 0, $1)));
f05e27e5 902 }
09b1cffe 903 | LSTOP optlistexpr /* print @args */
03d05f6e 904 { $$ = op_convert_list($1, 0, $2); }
09b1cffe 905 | FUNC '(' optexpr ')' /* print (@args) */
03d05f6e 906 { $$ = op_convert_list($1, 0, $3); }
718a7425 907 | LSTOPSUB startanonsub block /* sub f(&@); f { foo } ... */
5a5094bd 908 { SvREFCNT_inc_simple_void(PL_compcv);
a9f5ab8d 909 $<opval>$ = newANONATTRSUB($2, 0, NULL, $3); }
09b1cffe 910 optlistexpr %prec LSTOP /* ... @bar */
4633a7c4 911 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
2fcb4757
Z
912 op_append_elem(OP_LIST,
913 op_prepend_elem(OP_LIST, $<opval>4, $5), $1));
f05e27e5 914 }
a687059c
LW
915 ;
916
891be019 917/* Names of methods. May use $object->$methodname */
a0d0e21e
LW
918method : METHOD
919 | scalar
920 ;
921
891be019 922/* Some kind of subscripted expression */
89f35911 923subscripted: gelem '{' expr ';' '}' /* *main::{something} */
891be019
SC
924 /* In this and all the hash accessors, ';' is
925 * provided by the tokeniser */
5c86b6df 926 { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3)); }
891be019 927 | scalar '[' expr ']' /* $array[$element] */
f05e27e5 928 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3));
f05e27e5 929 }
891be019 930 | term ARROW '[' expr ']' /* somearef->[$element] */
fad39ff1
SM
931 { $$ = newBINOP(OP_AELEM, 0,
932 ref(newAVREF($1),OP_RV2AV),
f05e27e5 933 scalar($4));
f05e27e5 934 }
891be019 935 | subscripted '[' expr ']' /* $foo->[$bar]->[$baz] */
fad39ff1
SM
936 { $$ = newBINOP(OP_AELEM, 0,
937 ref(newAVREF($1),OP_RV2AV),
f05e27e5 938 scalar($3));
f05e27e5 939 }
5a63ceef 940 | scalar '{' expr ';' '}' /* $foo{bar();} */
fad39ff1 941 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
f05e27e5 942 }
891be019 943 | term ARROW '{' expr ';' '}' /* somehref->{bar();} */
fad39ff1
SM
944 { $$ = newBINOP(OP_HELEM, 0,
945 ref(newHVREF($1),OP_RV2HV),
5c86b6df 946 jmaybe($4)); }
891be019 947 | subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */
fad39ff1
SM
948 { $$ = newBINOP(OP_HELEM, 0,
949 ref(newHVREF($1),OP_RV2HV),
5c86b6df 950 jmaybe($3)); }
891be019 951 | term ARROW '(' ')' /* $subref->() */
fad39ff1 952 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
b5bbe64a 953 newCVREF(0, scalar($1))); }
891be019 954 | term ARROW '(' expr ')' /* $subref->(@args) */
fad39ff1 955 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
2fcb4757 956 op_append_elem(OP_LIST, $4,
b5bbe64a 957 newCVREF(0, scalar($1)))); }
fad39ff1 958
417a992d 959 | subscripted '(' expr ')' /* $foo->{bar}->(@args) */
fad39ff1 960 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
2fcb4757 961 op_append_elem(OP_LIST, $3,
b5bbe64a 962 newCVREF(0, scalar($1)))); }
417a992d 963 | subscripted '(' ')' /* $foo->{bar}->() */
fad39ff1 964 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
b5bbe64a 965 newCVREF(0, scalar($1))); }
9a9798c2 966 | '(' expr ')' '[' expr ']' /* list slice */
b5bbe64a 967 { $$ = newSLICEOP(0, $5, $2); }
ea25a9b2 968 | QWLIST '[' expr ']' /* list literal slice */
b5bbe64a 969 { $$ = newSLICEOP(0, $3, $1); }
9a9798c2 970 | '(' ')' '[' expr ']' /* empty list slice! */
a9f5ab8d 971 { $$ = newSLICEOP(0, $4, NULL); }
891be019 972 ;
fad39ff1 973
891be019 974/* Binary operators between terms */
3f5e9543 975termbinop: term ASSIGNOP term /* $x = $y, $x += $y */
b5bbe64a 976 { $$ = newASSIGNOP(OPf_STACKED, $1, $2, $3); }
891be019 977 | term POWOP term /* $x ** $y */
b5bbe64a 978 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
891be019 979 | term MULOP term /* $x * $y, $x x $y */
b5bbe64a 980 { if ($2 != OP_REPEAT)
79072805 981 scalar($1);
b5bbe64a 982 $$ = newBINOP($2, 0, $1, scalar($3));
f05e27e5 983 }
891be019 984 | term ADDOP term /* $x + $y */
b5bbe64a 985 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
891be019 986 | term SHIFTOP term /* $x >> $y, $x << $y */
b5bbe64a 987 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
891be019 988 | term RELOP term /* $x > $y, etc. */
b5bbe64a 989 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
891be019 990 | term EQOP term /* $x == $y, $x eq $y */
b5bbe64a 991 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
891be019 992 | term BITANDOP term /* $x & $y */
b5bbe64a 993 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
891be019 994 | term BITOROP term /* $x | $y */
b5bbe64a 995 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
891be019 996 | term DOTDOT term /* $x..$y, $x...$y */
b5bbe64a 997 { $$ = newRANGE($2, scalar($1), scalar($3)); }
891be019 998 | term ANDAND term /* $x && $y */
b5bbe64a 999 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
891be019 1000 | term OROR term /* $x || $y */
b5bbe64a 1001 { $$ = newLOGOP(OP_OR, 0, $1, $3); }
c963b151 1002 | term DORDOR term /* $x // $y */
b5bbe64a 1003 { $$ = newLOGOP(OP_DOR, 0, $1, $3); }
891be019 1004 | term MATCHOP term /* $x =~ /$y/ */
b5bbe64a 1005 { $$ = bind_match($2, $1, $3); }
891be019 1006 ;
8d063cd8 1007
891be019
SC
1008/* Unary operators and terms */
1009termunop : '-' term %prec UMINUS /* -$x */
b5bbe64a 1010 { $$ = newUNOP(OP_NEGATE, 0, scalar($2)); }
891be019 1011 | '+' term %prec UMINUS /* +$x */
b5bbe64a
JH
1012 { $$ = $2; }
1013
891be019 1014 | '!' term /* !$x */
b5bbe64a 1015 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
891be019 1016 | '~' term /* ~$x */
8823cb89 1017 { $$ = newUNOP($1, 0, scalar($2)); }
891be019 1018 | term POSTINC /* $x++ */
79072805 1019 { $$ = newUNOP(OP_POSTINC, 0,
b5bbe64a 1020 op_lvalue(scalar($1), OP_POSTINC)); }
891be019 1021 | term POSTDEC /* $x-- */
79072805 1022 { $$ = newUNOP(OP_POSTDEC, 0,
b5bbe64a 1023 op_lvalue(scalar($1), OP_POSTDEC));}
cc624add 1024 | term POSTJOIN /* implicit join after interpolated ->@ */
03d05f6e 1025 { $$ = op_convert_list(OP_JOIN, 0,
cc624add
FC
1026 op_append_elem(
1027 OP_LIST,
1028 newSVREF(scalar(
1029 newSVOP(OP_CONST,0,
1030 newSVpvs("\""))
1031 )),
1032 $1
1033 ));
cc624add 1034 }
891be019 1035 | PREINC term /* ++$x */
79072805 1036 { $$ = newUNOP(OP_PREINC, 0,
b5bbe64a 1037 op_lvalue(scalar($2), OP_PREINC)); }
891be019 1038 | PREDEC term /* --$x */
79072805 1039 { $$ = newUNOP(OP_PREDEC, 0,
b5bbe64a 1040 op_lvalue(scalar($2), OP_PREDEC)); }
891be019
SC
1041
1042 ;
1043
1044/* Constructors for anonymous data */
1045anonymous: '[' expr ']'
b5bbe64a 1046 { $$ = newANONLIST($2); }
891be019 1047 | '[' ']'
a9f5ab8d 1048 { $$ = newANONLIST(NULL);}
891be019 1049 | HASHBRACK expr ';' '}' %prec '(' /* { foo => "Bar" } */
b5bbe64a 1050 { $$ = newANONHASH($2); }
891be019 1051 | HASHBRACK ';' '}' %prec '(' /* { } (';' by tokener) */
a9f5ab8d 1052 { $$ = newANONHASH(NULL); }
abcf453d 1053 | ANONSUB startanonsub proto subattrlist block %prec '('
5a5094bd 1054 { SvREFCNT_inc_simple_void(PL_compcv);
b5bbe64a 1055 $$ = newANONATTRSUB($2, $3, $4, $5); }
abcf453d
PM
1056 | ANONSUB startanonsub remember subsignature subattrlist '{' stmtseq '}' %prec '('
1057 {
1058 OP *body;
1059 if (parser->copline > (line_t)$6)
1060 parser->copline = (line_t)$6;
1061 body = block_end($3,
1062 op_append_list(OP_LINESEQ, $4, $7));
1063 SvREFCNT_inc_simple_void(PL_compcv);
1064 $$ = newANONATTRSUB($2, NULL, $5, body);
1065 }
891be019
SC
1066
1067 ;
1068
1069/* Things called with "do" */
1070termdo : DO term %prec UNIOP /* do $filename */
b5bbe64a 1071 { $$ = dofile($2, $1);}
891be019 1072 | DO block %prec '(' /* do { code */
b5bbe64a 1073 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, op_scope($2));}
891be019
SC
1074 ;
1075
1076term : termbinop
1077 | termunop
1078 | anonymous
1079 | termdo
e9bdcc27 1080 | term '?' term ':' term
b5bbe64a 1081 { $$ = newCONDOP(0, $1, $3, $5); }
891be019 1082 | REFGEN term /* \$x, \@y, \%z */
070d3cb6 1083 { $$ = newUNOP(OP_REFGEN, 0, $2); }
e118fea3
FC
1084 | MY REFGEN term
1085 { $$ = newUNOP(OP_REFGEN, 0, localize($3,1)); }
09bef843
SB
1086 | myattrterm %prec UNIOP
1087 { $$ = $1; }
1088 | LOCAL term %prec UNIOP
28383d1a 1089 { $$ = localize($2,0); }
a0d0e21e 1090 | '(' expr ')'
b5bbe64a 1091 { $$ = sawparens($2); }
ea25a9b2 1092 | QWLIST
b5bbe64a 1093 { $$ = $1; }
8d063cd8 1094 | '(' ')'
b5bbe64a 1095 { $$ = sawparens(newNULLLIST()); }
79072805 1096 | scalar %prec '('
8d063cd8 1097 { $$ = $1; }
79072805 1098 | star %prec '('
8d063cd8 1099 { $$ = $1; }
79072805 1100 | hsh %prec '('
8d063cd8 1101 { $$ = $1; }
79072805 1102 | ary %prec '('
8d063cd8 1103 { $$ = $1; }
891be019 1104 | arylen %prec '(' /* $#x, $#{ something } */
79072805 1105 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
fad39ff1
SM
1106 | subscripted
1107 { $$ = $1; }
89f35911 1108 | sliceme '[' expr ']' /* array slice */
2fcb4757 1109 { $$ = op_prepend_elem(OP_ASLICE,
79072805 1110 newOP(OP_PUSHMARK, 0),
79072805
LW
1111 newLISTOP(OP_ASLICE, 0,
1112 list($3),
f05e27e5 1113 ref($1, OP_ASLICE)));
429a2555
FC
1114 if ($$ && $1)
1115 $$->op_private |=
1116 $1->op_private & OPpSLICEWARNING;
f05e27e5 1117 }
76eba8ab 1118 | kvslice '[' expr ']' /* array key/value slice */
6dd3e0f2
RZ
1119 { $$ = op_prepend_elem(OP_KVASLICE,
1120 newOP(OP_PUSHMARK, 0),
1121 newLISTOP(OP_KVASLICE, 0,
1122 list($3),
1123 ref(oopsAV($1), OP_KVASLICE)));
95a31aad
FC
1124 if ($$ && $1)
1125 $$->op_private |=
1126 $1->op_private & OPpSLICEWARNING;
6dd3e0f2 1127 }
89f35911 1128 | sliceme '{' expr ';' '}' /* @hash{@keys} */
2fcb4757 1129 { $$ = op_prepend_elem(OP_HSLICE,
79072805 1130 newOP(OP_PUSHMARK, 0),
79072805
LW
1131 newLISTOP(OP_HSLICE, 0,
1132 list($3),
a0d0e21e 1133 ref(oopsHV($1), OP_HSLICE)));
429a2555
FC
1134 if ($$ && $1)
1135 $$->op_private |=
1136 $1->op_private & OPpSLICEWARNING;
f05e27e5 1137 }
76eba8ab 1138 | kvslice '{' expr ';' '}' /* %hash{@keys} */
5cae3edb
RZ
1139 { $$ = op_prepend_elem(OP_KVHSLICE,
1140 newOP(OP_PUSHMARK, 0),
1141 newLISTOP(OP_KVHSLICE, 0,
1142 list($3),
1143 ref($1, OP_KVHSLICE)));
95a31aad
FC
1144 if ($$ && $1)
1145 $$->op_private |=
1146 $1->op_private & OPpSLICEWARNING;
5cae3edb 1147 }
79072805
LW
1148 | THING %prec '('
1149 { $$ = $1; }
891be019 1150 | amper /* &foo; */
c07a80fd 1151 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
fb602e32 1152 | amper '(' ')' /* &foo() or foo() */
f05e27e5 1153 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1));
f05e27e5 1154 }
fb602e32 1155 | amper '(' expr ')' /* &foo(@args) or foo(@args) */
f05e27e5
DM
1156 {
1157 $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
2fcb4757 1158 op_append_elem(OP_LIST, $3, scalar($1)));
f05e27e5 1159 }
fb602e32 1160 | NOAMP subname optlistexpr /* foo @args (no parens) */
a0d0e21e 1161 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
2fcb4757 1162 op_append_elem(OP_LIST, $3, scalar($2)));
f05e27e5 1163 }
89f35911 1164 | term ARROW '$' '*'
b5bbe64a 1165 { $$ = newSVREF($1); }
89f35911 1166 | term ARROW '@' '*'
b5bbe64a 1167 { $$ = newAVREF($1); }
89f35911 1168 | term ARROW '%' '*'
b5bbe64a 1169 { $$ = newHVREF($1); }
89f35911
FC
1170 | term ARROW '&' '*'
1171 { $$ = newUNOP(OP_ENTERSUB, 0,
b5bbe64a 1172 scalar(newCVREF($3,$1))); }
89f35911 1173 | term ARROW '*' '*' %prec '('
b5bbe64a 1174 { $$ = newGVREF(0,$1); }
891be019 1175 | LOOPEX /* loop exiting command (goto, last, dump, etc) */
b5bbe64a
JH
1176 { $$ = newOP($1, OPf_SPECIAL);
1177 PL_hints |= HINT_BLOCK_SCOPE; }
a0d0e21e 1178 | LOOPEX term
b5bbe64a 1179 { $$ = newLOOPEX($1,$2); }
09b1cffe 1180 | NOTOP listexpr /* not $foo */
b5bbe64a 1181 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
891be019 1182 | UNIOP /* Unary op, $_ implied */
b5bbe64a 1183 { $$ = newOP($1, 0); }
f05e27e5 1184 | UNIOP block /* eval { foo }* */
b5bbe64a 1185 { $$ = newUNOP($1, 0, $2); }
891be019 1186 | UNIOP term /* Unary op */
b5bbe64a 1187 { $$ = newUNOP($1, 0, $2); }
d2fdf8fd 1188 | REQUIRE /* require, $_ implied */
b5bbe64a 1189 { $$ = newOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0); }
d2fdf8fd 1190 | REQUIRE term /* require Foo */
b5bbe64a 1191 { $$ = newUNOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0, $2); }
3cd0a11a
RGS
1192 | UNIOPSUB
1193 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
891be019 1194 | UNIOPSUB term /* Sub treated as unop */
4633a7c4 1195 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
2fcb4757 1196 op_append_elem(OP_LIST, $2, scalar($1))); }
891be019 1197 | FUNC0 /* Nullary operator */
b5bbe64a 1198 { $$ = newOP($1, 0); }
ae986130 1199 | FUNC0 '(' ')'
b5bbe64a 1200 { $$ = newOP($1, 0);}
7eb971ee
FC
1201 | FUNC0OP /* Same as above, but op created in toke.c */
1202 { $$ = $1; }
1203 | FUNC0OP '(' ')'
b5bbe64a 1204 { $$ = $1; }
891be019 1205 | FUNC0SUB /* Sub treated as nullop */
b5bbe64a 1206 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
891be019 1207 | FUNC1 '(' ')' /* not () */
b5bbe64a
JH
1208 { $$ = ($1 == OP_NOT)
1209 ? newUNOP($1, 0, newSVOP(OP_CONST, 0, newSViv(0)))
1210 : newOP($1, OPf_SPECIAL); }
891be019 1211 | FUNC1 '(' expr ')' /* not($foo) */
b5bbe64a 1212 { $$ = newUNOP($1, 0, $3); }
d63c20f2
DM
1213 | PMFUNC /* m//, s///, qr//, tr/// */
1214 {
1215 if ( $1->op_type != OP_TRANS
1216 && $1->op_type != OP_TRANSR
1217 && (((PMOP*)$1)->op_pmflags & PMf_HAS_CV))
1218 {
1219 $<ival>$ = start_subparse(FALSE, CVf_ANON);
1220 SAVEFREESV(PL_compcv);
1221 } else
1222 $<ival>$ = 0;
1223 }
9b6b7be8
FC
1224 '(' listexpr optrepl ')'
1225 { $$ = pmruntime($1, $4, $5, 1, $<ival>2); }
185c2e96 1226 | BAREWORD
378cc40b 1227 | listop
88e1f1a2 1228 | PLUGEXPR
8d063cd8
LW
1229 ;
1230
891be019 1231/* "my" declarations, with optional attributes */
09bef843 1232myattrterm: MY myterm myattrlist
b5bbe64a 1233 { $$ = my_attrs($2,$3); }
09bef843 1234 | MY myterm
28383d1a 1235 { $$ = localize($2,1); }
e118fea3
FC
1236 | MY REFGEN myterm myattrlist
1237 { $$ = newUNOP(OP_REFGEN, 0, my_attrs($3,$4)); }
09bef843
SB
1238 ;
1239
891be019 1240/* Things that can be "my"'d */
09bef843 1241myterm : '(' expr ')'
b5bbe64a 1242 { $$ = sawparens($2); }
09bef843 1243 | '(' ')'
b5bbe64a
JH
1244 { $$ = sawparens(newNULLLIST()); }
1245
09bef843
SB
1246 | scalar %prec '('
1247 { $$ = $1; }
1248 | hsh %prec '('
1249 { $$ = $1; }
1250 | ary %prec '('
1251 { $$ = $1; }
1252 ;
1253
891be019 1254/* Basic list expressions */
09b1cffe 1255optlistexpr: /* NULL */ %prec PREC_LOW
a9f5ab8d 1256 { $$ = NULL; }
09b1cffe 1257 | listexpr %prec PREC_LOW
a0d0e21e
LW
1258 { $$ = $1; }
1259 ;
1260
09b1cffe 1261optexpr: /* NULL */
a9f5ab8d 1262 { $$ = NULL; }
79072805
LW
1263 | expr
1264 { $$ = $1; }
1265 ;
1266
9b6b7be8 1267optrepl: /* NULL */
a9f5ab8d 1268 { $$ = NULL; }
9b6b7be8
FC
1269 | '/' expr
1270 { $$ = $2; }
1271 ;
1272
891be019
SC
1273/* A little bit of trickery to make "for my $foo (@bar)" actually be
1274 lexical */
55497cff 1275my_scalar: scalar
624fa8bd 1276 { parser->in_my = 0; $$ = my($1); }
55497cff 1277 ;
1278
d39c26a6
FC
1279my_var : scalar
1280 | ary
1281 | hsh
1282 ;
1283
1284refgen_topic: my_var
1285 | amper
1286 ;
1287
e118fea3
FC
1288my_refgen: MY REFGEN
1289 | REFGEN MY
1290 ;
1291
79072805 1292amper : '&' indirob
b5bbe64a 1293 { $$ = newCVREF($1,$2); }
a687059c
LW
1294 ;
1295
79072805 1296scalar : '$' indirob
b5bbe64a 1297 { $$ = newSVREF($2); }
a687059c
LW
1298 ;
1299
79072805 1300ary : '@' indirob
f05e27e5 1301 { $$ = newAVREF($2);
b5bbe64a 1302 if ($$) $$->op_private |= $1;
f05e27e5 1303 }
79072805
LW
1304 ;
1305
1306hsh : '%' indirob
f05e27e5 1307 { $$ = newHVREF($2);
b5bbe64a 1308 if ($$) $$->op_private |= $1;
f05e27e5 1309 }
79072805
LW
1310 ;
1311
1312arylen : DOLSHARP indirob
b5bbe64a 1313 { $$ = newAVREF($2); }
ff25e5db 1314 | term ARROW DOLSHARP '*'
b5bbe64a 1315 { $$ = newAVREF($1); }
79072805
LW
1316 ;
1317
1318star : '*' indirob
b5bbe64a 1319 { $$ = newGVREF(0,$2); }
79072805
LW
1320 ;
1321
89f35911
FC
1322sliceme : ary
1323 | term ARROW '@'
b5bbe64a 1324 { $$ = newAVREF($1); }
89f35911
FC
1325 ;
1326
76eba8ab
FC
1327kvslice : hsh
1328 | term ARROW '%'
b5bbe64a 1329 { $$ = newHVREF($1); }
76eba8ab
FC
1330 ;
1331
89f35911
FC
1332gelem : star
1333 | term ARROW '*'
b5bbe64a 1334 { $$ = newGVREF(0,$1); }
89f35911
FC
1335 ;
1336
891be019 1337/* Indirect objects */
185c2e96 1338indirob : BAREWORD
79072805 1339 { $$ = scalar($1); }
fad39ff1 1340 | scalar %prec PREC_LOW
f05e27e5 1341 { $$ = scalar($1); }
79072805 1342 | block
3ad73efd 1343 { $$ = op_scope($1); }
79072805 1344
93a17b20
LW
1345 | PRIVATEREF
1346 { $$ = $1; }
8d063cd8 1347 ;