This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
parse subs and signature subs separately
[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 53%token <pval> LABEL
436ddf68 54%token <ival> FORMAT SUB SIGSUB ANONSUB ANON_SIGSUB PACKAGE USE
b5bbe64a 55%token <ival> WHILE UNTIL IF UNLESS ELSE ELSIF CONTINUE FOR
7896dde7 56%token <ival> GIVEN WHEN DEFAULT
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
894f226e 77%type <opval> realsubbody termbinop termunop anonymous termdo
d3d9da4a
DM
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
PP
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
PP
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 {
50333c97 278 init_named_cv(PL_compcv, $2);
624fa8bd
FC
279 parser->in_my = 0;
280 parser->in_my_stash = NULL;
764212cf 281 }
30d9c59b 282 proto subattrlist optsubbody
eae48c89
Z
283 {
284 SvREFCNT_inc_simple_void(PL_compcv);
50278755
FC
285 $2->op_type == OP_CONST
286 ? newATTRSUB($3, $2, $5, $6, $7)
287 : newMYSUB($3, $2, $5, $6, $7)
50278755 288 ;
a9f5ab8d 289 $$ = NULL;
764212cf 290 intro_my();
34b54951 291 parser->parsed_sub = 1;
f05e27e5 292 }
436ddf68
DM
293 | SIGSUB subname startsub
294 {
295 init_named_cv(PL_compcv, $2);
296 parser->in_my = 0;
297 parser->in_my_stash = NULL;
298 }
299 proto subattrlist optsubbody
300 {
301 SvREFCNT_inc_simple_void(PL_compcv);
302 $2->op_type == OP_CONST
303 ? newATTRSUB($3, $2, $5, $6, $7)
304 : newMYSUB($3, $2, $5, $6, $7)
305 ;
306 $$ = NULL;
307 intro_my();
308 parser->parsed_sub = 1;
309 }
185c2e96 310 | PACKAGE BAREWORD BAREWORD ';'
5f211341 311 {
eae48c89
Z
312 package($3);
313 if ($2)
314 package_version($2);
a9f5ab8d 315 $$ = NULL;
eae48c89
Z
316 }
317 | USE startsub
318 { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
185c2e96 319 BAREWORD BAREWORD optlistexpr ';'
eae48c89
Z
320 {
321 SvREFCNT_inc_simple_void(PL_compcv);
b5bbe64a 322 utilize($1, $2, $4, $5, $6);
34b54951 323 parser->parsed_sub = 1;
a9f5ab8d 324 $$ = NULL;
eae48c89 325 }
417a992d 326 | IF '(' remember mexpr ')' mblock else
eae48c89 327 {
3ad73efd
Z
328 $$ = block_end($3,
329 newCONDOP(0, $4, op_scope($6), $7));
624fa8bd 330 parser->copline = (line_t)$1;
eae48c89 331 }
08b3e84f 332 | UNLESS '(' remember mexpr ')' mblock else
eae48c89 333 {
3ad73efd 334 $$ = block_end($3,
08b3e84f 335 newCONDOP(0, $4, $7, op_scope($6)));
624fa8bd 336 parser->copline = (line_t)$1;
eae48c89 337 }
b5a64814 338 | GIVEN '(' remember mexpr ')' mblock
eae48c89 339 {
5d051ee0 340 $$ = block_end($3, newGIVENOP($4, op_scope($6), 0));
624fa8bd 341 parser->copline = (line_t)$1;
eae48c89 342 }
7896dde7
Z
343 | WHEN '(' remember mexpr ')' mblock
344 { $$ = block_end($3, newWHENOP($4, op_scope($6))); }
345 | DEFAULT block
346 { $$ = newWHENOP(0, op_scope($2)); }
417a992d 347 | WHILE '(' remember texpr ')' mintro mblock cont
eae48c89
Z
348 {
349 $$ = block_end($3,
a9f5ab8d 350 newWHILEOP(0, 1, NULL,
94bf0465 351 $4, $7, $8, $6));
624fa8bd 352 parser->copline = (line_t)$1;
eae48c89 353 }
417a992d 354 | UNTIL '(' remember iexpr ')' mintro mblock cont
eae48c89
Z
355 {
356 $$ = block_end($3,
a9f5ab8d 357 newWHILEOP(0, 1, NULL,
94bf0465 358 $4, $7, $8, $6));
624fa8bd 359 parser->copline = (line_t)$1;
eae48c89 360 }
2d0e3c96
FC
361 | FOR '(' remember mnexpr ';'
362 { parser->expect = XTERM; }
363 texpr ';'
364 { parser->expect = XTERM; }
365 mintro mnexpr ')'
eae48c89
Z
366 mblock
367 {
b5bbe64a 368 OP *initop = $4;
a9f5ab8d 369 OP *forop = newWHILEOP(0, 1, NULL,
2d0e3c96 370 scalar($7), $13, $11, $10);
eae48c89
Z
371 if (initop) {
372 forop = op_prepend_elem(OP_LINESEQ, initop,
373 op_append_elem(OP_LINESEQ,
374 newOP(OP_UNSTACK, OPf_SPECIAL),
375 forop));
5f211341 376 }
0f602692 377 PL_hints |= HINT_BLOCK_SCOPE;
eae48c89 378 $$ = block_end($3, forop);
624fa8bd 379 parser->copline = (line_t)$1;
eae48c89 380 }
417a992d 381 | FOR MY remember my_scalar '(' mexpr ')' mblock cont
eae48c89 382 {
94bf0465 383 $$ = block_end($3, newFOROP(0, $4, $6, $8, $9));
624fa8bd 384 parser->copline = (line_t)$1;
eae48c89 385 }
417a992d 386 | FOR scalar '(' remember mexpr ')' mblock cont
eae48c89 387 {
94bf0465 388 $$ = block_end($4, newFOROP(0,
3ad73efd 389 op_lvalue($2, OP_ENTERLOOP), $5, $7, $8));
624fa8bd 390 parser->copline = (line_t)$1;
eae48c89 391 }
e118fea3
FC
392 | FOR my_refgen remember my_var
393 { parser->in_my = 0; $<opval>$ = my($4); }
d39c26a6
FC
394 '(' mexpr ')' mblock cont
395 {
396 $$ = block_end(
e118fea3 397 $3,
d39c26a6
FC
398 newFOROP(0,
399 op_lvalue(
400 newUNOP(OP_REFGEN, 0,
e118fea3 401 $<opval>5),
d39c26a6 402 OP_ENTERLOOP),
e118fea3 403 $7, $9, $10)
d39c26a6
FC
404 );
405 parser->copline = (line_t)$1;
406 }
407 | FOR REFGEN refgen_topic '(' remember mexpr ')' mblock cont
408 {
409 $$ = block_end($5, newFOROP(
410 0, op_lvalue(newUNOP(OP_REFGEN, 0,
96801525 411 $3),
d39c26a6
FC
412 OP_ENTERLOOP), $6, $8, $9));
413 parser->copline = (line_t)$1;
414 }
417a992d 415 | FOR '(' remember mexpr ')' mblock cont
eae48c89
Z
416 {
417 $$ = block_end($3,
a9f5ab8d 418 newFOROP(0, NULL, $4, $6, $7));
624fa8bd 419 parser->copline = (line_t)$1;
eae48c89
Z
420 }
421 | block cont
422 {
423 /* a block is a loop that happens once */
a9f5ab8d
LM
424 $$ = newWHILEOP(0, 1, NULL,
425 NULL, $1, $2, 0);
eae48c89 426 }
185c2e96 427 | PACKAGE BAREWORD BAREWORD '{' remember
eae48c89 428 {
eae48c89 429 package($3);
eae48c89 430 if ($2) {
eae48c89 431 package_version($2);
eae48c89
Z
432 }
433 }
434 stmtseq '}'
435 {
436 /* a block is a loop that happens once */
a9f5ab8d
LM
437 $$ = newWHILEOP(0, 1, NULL,
438 NULL, block_end($5, $7), NULL, 0);
624fa8bd
FC
439 if (parser->copline > (line_t)$4)
440 parser->copline = (line_t)$4;
eae48c89
Z
441 }
442 | sideff ';'
443 {
eae48c89 444 $$ = $1;
eae48c89 445 }
29d69c3c
Z
446 | YADAYADA ';'
447 {
448 $$ = newLISTOP(OP_DIE, 0, newOP(OP_PUSHMARK, 0),
449 newSVOP(OP_CONST, 0, newSVpvs("Unimplemented")));
450 }
eae48c89
Z
451 | ';'
452 {
a9f5ab8d 453 $$ = NULL;
624fa8bd 454 parser->copline = NOLINE;
5f211341 455 }
8d063cd8
LW
456 ;
457
705fe0e5
FC
458/* Format line */
459formline: THING formarg
460 { OP *list;
461 if ($2) {
462 OP *term = $2;
705fe0e5
FC
463 list = op_append_elem(OP_LIST, $1, term);
464 }
465 else {
705fe0e5 466 list = $1;
705fe0e5 467 }
624fa8bd
FC
468 if (parser->copline == NOLINE)
469 parser->copline = CopLINE(PL_curcop)-1;
470 else parser->copline--;
705fe0e5 471 $$ = newSTATEOP(0, NULL,
03d05f6e 472 op_convert_list(OP_FORMLINE, 0, list));
705fe0e5
FC
473 }
474 ;
475
476formarg : /* NULL */
477 { $$ = NULL; }
478 | FORMLBRACK stmtseq FORMRBRACK
479 { $$ = op_unscope($2); }
480 ;
481
891be019 482/* An expression which may have a side-effect */
a687059c 483sideff : error
a9f5ab8d 484 { $$ = NULL; }
a687059c 485 | expr
79072805 486 { $$ = $1; }
a687059c 487 | expr IF expr
b5bbe64a 488 { $$ = newLOGOP(OP_AND, 0, $3, $1); }
a687059c 489 | expr UNLESS expr
b5bbe64a 490 { $$ = newLOGOP(OP_OR, 0, $3, $1); }
a687059c 491 | expr WHILE expr
b5bbe64a 492 { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1); }
55497cff 493 | expr UNTIL iexpr
b5bbe64a 494 { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1); }
ecca16b0 495 | expr FOR expr
a9f5ab8d 496 { $$ = newFOROP(0, NULL, $3, $1, NULL);
624fa8bd 497 parser->copline = (line_t)$2; }
7896dde7
Z
498 | expr WHEN expr
499 { $$ = newWHENOP($3, op_scope($1)); }
79072805
LW
500 ;
501
891be019 502/* else and elsif blocks */
79072805 503else : /* NULL */
a9f5ab8d 504 { $$ = NULL; }
55497cff 505 | ELSE mblock
3ad73efd
Z
506 {
507 ($2)->op_flags |= OPf_PARENS;
508 $$ = op_scope($2);
f05e27e5 509 }
417a992d 510 | ELSIF '(' mexpr ')' mblock else
624fa8bd 511 { parser->copline = (line_t)$1;
3ad73efd
Z
512 $$ = newCONDOP(0,
513 newSTATEOP(OPf_SPECIAL,NULL,$3),
514 op_scope($5), $6);
515 PL_hints |= HINT_BLOCK_SCOPE;
f05e27e5 516 }
79072805
LW
517 ;
518
891be019 519/* Continue blocks */
79072805 520cont : /* NULL */
a9f5ab8d 521 { $$ = NULL; }
79072805 522 | CONTINUE block
b5bbe64a 523 { $$ = op_scope($2); }
79072805
LW
524 ;
525
a034e688
DM
526/* determine whether there are any new my declarations */
527mintro : /* NULL */
528 { $$ = (PL_min_intro_pending &&
529 PL_max_intro_pending >= PL_min_intro_pending);
530 intro_my(); }
531
891be019 532/* Normal expression */
8d063cd8 533nexpr : /* NULL */
a9f5ab8d 534 { $$ = NULL; }
8d063cd8
LW
535 | sideff
536 ;
537
891be019 538/* Boolean expression */
8d063cd8 539texpr : /* NULL means true */
f05e27e5
DM
540 { YYSTYPE tmplval;
541 (void)scan_num("1", &tmplval);
542 $$ = tmplval.opval; }
8d063cd8
LW
543 | expr
544 ;
545
891be019 546/* Inverted boolean expression */
55497cff
PP
547iexpr : expr
548 { $$ = invert(scalar($1)); }
549 ;
550
891be019 551/* Expression with its own lexical scope */
55497cff 552mexpr : expr
bbce6d69
PP
553 { $$ = $1; intro_my(); }
554 ;
555
556mnexpr : nexpr
557 { $$ = $1; intro_my(); }
55497cff
PP
558 ;
559
185c2e96 560formname: BAREWORD { $$ = $1; }
a9f5ab8d 561 | /* NULL */ { $$ = NULL; }
8d063cd8
LW
562 ;
563
fa83b5b6 564startsub: /* NULL */ /* start a regular subroutine scope */
a8ff2fa6
DM
565 { $$ = start_subparse(FALSE, 0);
566 SAVEFREESV(PL_compcv); }
f05e27e5 567
28757baa
PP
568 ;
569
570startanonsub: /* NULL */ /* start an anonymous subroutine scope */
a8ff2fa6
DM
571 { $$ = start_subparse(FALSE, CVf_ANON);
572 SAVEFREESV(PL_compcv); }
28757baa
PP
573 ;
574
44a8e56a 575startformsub: /* NULL */ /* start a format subroutine scope */
a8ff2fa6
DM
576 { $$ = start_subparse(TRUE, 0);
577 SAVEFREESV(PL_compcv); }
44a8e56a
PP
578 ;
579
891be019 580/* Name of a subroutine - must be a bareword, could be special */
185c2e96 581subname : BAREWORD
4210d3f1 582 | PRIVATEREF
a0d0e21e
LW
583 ;
584
891be019 585/* Subroutine prototype */
4633a7c4 586proto : /* NULL */
a9f5ab8d 587 { $$ = NULL; }
4633a7c4
LW
588 | THING
589 ;
28757baa 590
891be019 591/* Optional list of subroutine attributes */
09bef843 592subattrlist: /* NULL */
a9f5ab8d 593 { $$ = NULL; }
09bef843 594 | COLONATTR THING
b5bbe64a 595 { $$ = $2; }
09bef843 596 | COLONATTR
a9f5ab8d 597 { $$ = NULL; }
09bef843
SB
598 ;
599
891be019 600/* List of attributes for a "my" variable declaration */
09bef843 601myattrlist: COLONATTR THING
b5bbe64a 602 { $$ = $2; }
09bef843 603 | COLONATTR
a9f5ab8d 604 { $$ = NULL; }
09bef843
SB
605 ;
606
abcf453d 607
d3d9da4a
DM
608
609/* --------------------------------------
610 * subroutine signature parsing
611 */
612
613/* the '' or 'foo' part of a '$' or '@foo' etc signature variable */
49fb8620 614sigvarname: /* NULL */
a9f5ab8d 615 { parser->in_my = 0; $$ = NULL; }
d3d9da4a 616 | PRIVATEREF
49fb8620 617 { parser->in_my = 0; $$ = $1; }
d3d9da4a
DM
618 ;
619
620sigslurpsigil:
621 '@'
622 { $$ = '@'; }
623 | '%'
624 { $$ = '%'; }
625
626/* @, %, @foo, %foo */
627sigslurpelem: sigslurpsigil sigvarname sigdefault/* def only to catch errors */
628 {
4fa06845
DM
629 I32 sigil = $1;
630 OP *var = $2;
d3d9da4a 631 OP *defexpr = $3;
d3d9da4a 632
036bbc13 633 if (parser->sig_slurpy)
d3d9da4a 634 yyerror("Multiple slurpy parameters not allowed");
036bbc13 635 parser->sig_slurpy = (char)sigil;
d3d9da4a
DM
636
637 if (defexpr)
bb6b75cd 638 yyerror("A slurpy parameter may not have "
d3d9da4a
DM
639 "a default value");
640
a9f5ab8d 641 $$ = var ? newSTATEOP(0, NULL, var) : NULL;
d3d9da4a
DM
642 }
643 ;
644
645/* default part of sub signature scalar element: i.e. '= default_expr' */
646sigdefault: /* NULL */
a9f5ab8d 647 { $$ = NULL; }
d3d9da4a
DM
648 | ASSIGNOP
649 { $$ = newOP(OP_NULL, 0); }
650 | ASSIGNOP term
651 { $$ = $2; }
652
653
654/* subroutine signature scalar element: e.g. '$x', '$=', '$x = $default' */
655sigscalarelem:
656 '$' sigvarname sigdefault
657 {
658 OP *var = $2;
659 OP *defexpr = $3;
d3d9da4a 660
036bbc13 661 if (parser->sig_slurpy)
d3d9da4a
DM
662 yyerror("Slurpy parameter not last");
663
036bbc13 664 parser->sig_elems++;
d3d9da4a 665
d3d9da4a 666 if (defexpr) {
036bbc13 667 parser->sig_optelems++;
4fa06845
DM
668
669 if ( defexpr->op_type == OP_NULL
d3d9da4a
DM
670 && !(defexpr->op_flags & OPf_KIDS))
671 {
4fa06845
DM
672 /* handle '$=' special case */
673 if (var)
674 yyerror("Optional parameter "
675 "lacks default expression");
d3d9da4a
DM
676 op_free(defexpr);
677 }
4fa06845
DM
678 else {
679 /* a normal '=default' expression */
680 OP *defop = (OP*)alloc_LOGOP(OP_ARGDEFELEM,
681 defexpr,
682 LINKLIST(defexpr));
683 /* re-purpose op_targ to hold @_ index */
6daeaaa3 684 defop->op_targ =
036bbc13 685 (PADOFFSET)(parser->sig_elems - 1);
4fa06845
DM
686
687 if (var) {
688 var->op_flags |= OPf_STACKED;
689 (void)op_sibling_splice(var,
690 NULL, 0, defop);
691 scalar(defop);
692 }
693 else
694 var = newUNOP(OP_NULL, 0, defop);
695
696 LINKLIST(var);
697 /* NB: normally the first child of a
698 * logop is executed before the logop,
699 * and it pushes a boolean result
700 * ready for the logop. For ARGDEFELEM,
701 * the op itself does the boolean
702 * calculation, so set the first op to
703 * it instead.
704 */
705 var->op_next = defop;
706 defexpr->op_next = var;
d3d9da4a
DM
707 }
708 }
709 else {
036bbc13 710 if (parser->sig_optelems)
d3d9da4a
DM
711 yyerror("Mandatory parameter "
712 "follows optional parameter");
d3d9da4a
DM
713 }
714
a9f5ab8d 715 $$ = var ? newSTATEOP(0, NULL, var) : NULL;
d3d9da4a
DM
716 }
717 ;
718
719
720/* subroutine signature element: e.g. '$x = $default' or '%h' */
721sigelem: sigscalarelem
49fb8620 722 { parser->in_my = KEY_sigvar; $$ = $1; }
d3d9da4a 723 | sigslurpelem
49fb8620 724 { parser->in_my = KEY_sigvar; $$ = $1; }
d3d9da4a
DM
725 ;
726
727/* list of subroutine signature elements */
728siglist:
729 siglist ','
730 { $$ = $1; }
731 | siglist ',' sigelem
30d9c59b 732 {
d3d9da4a 733 $$ = op_append_list(OP_LINESEQ, $1, $3);
30d9c59b 734 }
d3d9da4a
DM
735 | sigelem %prec PREC_LOW
736 { $$ = $1; }
30d9c59b
Z
737 ;
738
d3d9da4a
DM
739/* () or (....) */
740siglistornull: /* NULL */
a9f5ab8d 741 { $$ = NULL; }
d3d9da4a
DM
742 | siglist
743 { $$ = $1; }
744
745/* Subroutine signature */
894f226e
DM
746subsignature: /* NULL */ { $$ = (OP*)NULL; }
747 | '('
d3d9da4a
DM
748 {
749 ENTER;
036bbc13
FC
750 SAVEIV(parser->sig_elems);
751 SAVEIV(parser->sig_optelems);
752 SAVEI8(parser->sig_slurpy);
753 parser->sig_elems = 0;
754 parser->sig_optelems = 0;
755 parser->sig_slurpy = 0;
49fb8620 756 parser->in_my = KEY_sigvar;
d3d9da4a
DM
757 }
758 siglistornull
759 ')'
760 {
4fa06845
DM
761 OP *sigops = $3;
762 UNOP_AUX_item *aux;
763 OP *check;
d3d9da4a 764
894f226e
DM
765 if (!FEATURE_SIGNATURES_IS_ENABLED)
766 Perl_croak(aTHX_ "Experimental "
767 "subroutine signatures not enabled");
d3d9da4a
DM
768
769 /* We shouldn't get here otherwise */
770 Perl_ck_warner_d(aTHX_
771 packWARN(WARN_EXPERIMENTAL__SIGNATURES),
772 "The signatures feature is experimental");
773
4fa06845
DM
774 aux = (UNOP_AUX_item*)PerlMemShared_malloc(
775 sizeof(UNOP_AUX_item) * 3);
036bbc13
FC
776 aux[0].iv = parser->sig_elems;
777 aux[1].iv = parser->sig_optelems;
778 aux[2].iv = parser->sig_slurpy;
4fa06845
DM
779 check = newUNOP_AUX(OP_ARGCHECK, 0, NULL, aux);
780 sigops = op_prepend_elem(OP_LINESEQ, check, sigops);
781 sigops = op_prepend_elem(OP_LINESEQ,
782 newSTATEOP(0, NULL, NULL),
783 sigops);
784 /* a nextstate at the end handles context
785 * correctly for an empty sub body */
786 $$ = op_append_elem(OP_LINESEQ,
787 sigops,
788 newSTATEOP(0, NULL, NULL));
d3d9da4a 789
49fb8620 790 parser->in_my = 0;
894f226e 791 parser->expect = XBLOCK;
d3d9da4a
DM
792 LEAVE;
793 }
794 ;
795
796
894f226e
DM
797/* Subroutine body - block with optional signature */
798realsubbody: remember subsignature '{' stmtseq '}'
799 {
800 if (parser->copline > (line_t)$3)
801 parser->copline = (line_t)$3;
802 $$ = block_end($1,
803 op_append_list(OP_LINESEQ, $2, $4));
804 }
805 ;
806
d3d9da4a 807
30d9c59b 808/* Optional subroutine body, for named subroutine declaration */
894f226e 809optsubbody: realsubbody { $$ = $1; }
a9f5ab8d 810 | ';' { $$ = NULL; }
8d063cd8
LW
811 ;
812
891be019 813/* Ordinary expressions; logical combinations */
a0d0e21e 814expr : expr ANDOP expr
b5bbe64a 815 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
a0d0e21e 816 | expr OROP expr
b5bbe64a 817 { $$ = newLOGOP($2, 0, $1, $3); }
c963b151 818 | expr DOROP expr
b5bbe64a 819 { $$ = newLOGOP(OP_DOR, 0, $1, $3); }
09b1cffe 820 | listexpr %prec PREC_LOW
a0d0e21e
LW
821 ;
822
891be019 823/* Expressions are a list of terms joined by commas */
09b1cffe 824listexpr: listexpr ','
b5bbe64a 825 { $$ = $1; }
09b1cffe 826 | listexpr ',' term
abcf453d 827 {
29522234 828 OP* term = $3;
2fcb4757 829 $$ = op_append_elem(OP_LIST, $1, term);
f05e27e5 830 }
fad39ff1 831 | term %prec PREC_LOW
8d063cd8
LW
832 ;
833
891be019 834/* List operators */
09b1cffe 835listop : LSTOP indirob listexpr /* map {...} @args or print $fh @args */
03d05f6e 836 { $$ = op_convert_list($1, OPf_STACKED,
b5bbe64a 837 op_prepend_elem(OP_LIST, newGVREF($1,$2), $3) );
f05e27e5 838 }
891be019 839 | FUNC '(' indirob expr ')' /* print ($fh @args */
03d05f6e 840 { $$ = op_convert_list($1, OPf_STACKED,
b5bbe64a 841 op_prepend_elem(OP_LIST, newGVREF($1,$3), $4) );
f05e27e5 842 }
417a992d 843 | term ARROW method '(' optexpr ')' /* $foo->bar(list) */
03d05f6e 844 { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
2fcb4757
Z
845 op_append_elem(OP_LIST,
846 op_prepend_elem(OP_LIST, scalar($1), $5),
b46e009d 847 newMETHOP(OP_METHOD, 0, $3)));
f05e27e5 848 }
891be019 849 | term ARROW method /* $foo->bar */
03d05f6e 850 { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
2fcb4757 851 op_append_elem(OP_LIST, scalar($1),
b46e009d 852 newMETHOP(OP_METHOD, 0, $3)));
f05e27e5 853 }
09b1cffe 854 | METHOD indirob optlistexpr /* new Class @args */
03d05f6e 855 { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
2fcb4757
Z
856 op_append_elem(OP_LIST,
857 op_prepend_elem(OP_LIST, $2, $3),
b46e009d 858 newMETHOP(OP_METHOD, 0, $1)));
f05e27e5 859 }
09b1cffe 860 | FUNCMETH indirob '(' optexpr ')' /* method $object (@args) */
03d05f6e 861 { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
2fcb4757
Z
862 op_append_elem(OP_LIST,
863 op_prepend_elem(OP_LIST, $2, $4),
b46e009d 864 newMETHOP(OP_METHOD, 0, $1)));
f05e27e5 865 }
09b1cffe 866 | LSTOP optlistexpr /* print @args */
03d05f6e 867 { $$ = op_convert_list($1, 0, $2); }
09b1cffe 868 | FUNC '(' optexpr ')' /* print (@args) */
03d05f6e 869 { $$ = op_convert_list($1, 0, $3); }
718a7425 870 | LSTOPSUB startanonsub block /* sub f(&@); f { foo } ... */
5a5094bd 871 { SvREFCNT_inc_simple_void(PL_compcv);
a9f5ab8d 872 $<opval>$ = newANONATTRSUB($2, 0, NULL, $3); }
09b1cffe 873 optlistexpr %prec LSTOP /* ... @bar */
4633a7c4 874 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
2fcb4757
Z
875 op_append_elem(OP_LIST,
876 op_prepend_elem(OP_LIST, $<opval>4, $5), $1));
f05e27e5 877 }
a687059c
LW
878 ;
879
891be019 880/* Names of methods. May use $object->$methodname */
a0d0e21e
LW
881method : METHOD
882 | scalar
883 ;
884
891be019 885/* Some kind of subscripted expression */
89f35911 886subscripted: gelem '{' expr ';' '}' /* *main::{something} */
891be019
SC
887 /* In this and all the hash accessors, ';' is
888 * provided by the tokeniser */
5c86b6df 889 { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3)); }
891be019 890 | scalar '[' expr ']' /* $array[$element] */
f05e27e5 891 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3));
f05e27e5 892 }
891be019 893 | term ARROW '[' expr ']' /* somearef->[$element] */
fad39ff1
SM
894 { $$ = newBINOP(OP_AELEM, 0,
895 ref(newAVREF($1),OP_RV2AV),
f05e27e5 896 scalar($4));
f05e27e5 897 }
891be019 898 | subscripted '[' expr ']' /* $foo->[$bar]->[$baz] */
fad39ff1
SM
899 { $$ = newBINOP(OP_AELEM, 0,
900 ref(newAVREF($1),OP_RV2AV),
f05e27e5 901 scalar($3));
f05e27e5 902 }
5a63ceef 903 | scalar '{' expr ';' '}' /* $foo{bar();} */
fad39ff1 904 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
f05e27e5 905 }
891be019 906 | term ARROW '{' expr ';' '}' /* somehref->{bar();} */
fad39ff1
SM
907 { $$ = newBINOP(OP_HELEM, 0,
908 ref(newHVREF($1),OP_RV2HV),
5c86b6df 909 jmaybe($4)); }
891be019 910 | subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */
fad39ff1
SM
911 { $$ = newBINOP(OP_HELEM, 0,
912 ref(newHVREF($1),OP_RV2HV),
5c86b6df 913 jmaybe($3)); }
891be019 914 | term ARROW '(' ')' /* $subref->() */
fad39ff1 915 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
097ff42c
Z
916 newCVREF(0, scalar($1)));
917 if (parser->expect == XBLOCK)
918 parser->expect = XOPERATOR;
919 }
891be019 920 | term ARROW '(' expr ')' /* $subref->(@args) */
fad39ff1 921 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
2fcb4757 922 op_append_elem(OP_LIST, $4,
097ff42c
Z
923 newCVREF(0, scalar($1))));
924 if (parser->expect == XBLOCK)
925 parser->expect = XOPERATOR;
926 }
fad39ff1 927
417a992d 928 | subscripted '(' expr ')' /* $foo->{bar}->(@args) */
fad39ff1 929 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
2fcb4757 930 op_append_elem(OP_LIST, $3,
097ff42c
Z
931 newCVREF(0, scalar($1))));
932 if (parser->expect == XBLOCK)
933 parser->expect = XOPERATOR;
934 }
417a992d 935 | subscripted '(' ')' /* $foo->{bar}->() */
fad39ff1 936 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
097ff42c
Z
937 newCVREF(0, scalar($1)));
938 if (parser->expect == XBLOCK)
939 parser->expect = XOPERATOR;
940 }
9a9798c2 941 | '(' expr ')' '[' expr ']' /* list slice */
b5bbe64a 942 { $$ = newSLICEOP(0, $5, $2); }
ea25a9b2 943 | QWLIST '[' expr ']' /* list literal slice */
b5bbe64a 944 { $$ = newSLICEOP(0, $3, $1); }
9a9798c2 945 | '(' ')' '[' expr ']' /* empty list slice! */
a9f5ab8d 946 { $$ = newSLICEOP(0, $4, NULL); }
891be019 947 ;
fad39ff1 948
891be019 949/* Binary operators between terms */
3f5e9543 950termbinop: term ASSIGNOP term /* $x = $y, $x += $y */
b5bbe64a 951 { $$ = newASSIGNOP(OPf_STACKED, $1, $2, $3); }
891be019 952 | term POWOP term /* $x ** $y */
b5bbe64a 953 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
891be019 954 | term MULOP term /* $x * $y, $x x $y */
b5bbe64a 955 { if ($2 != OP_REPEAT)
79072805 956 scalar($1);
b5bbe64a 957 $$ = newBINOP($2, 0, $1, scalar($3));
f05e27e5 958 }
891be019 959 | term ADDOP term /* $x + $y */
b5bbe64a 960 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
891be019 961 | term SHIFTOP term /* $x >> $y, $x << $y */
b5bbe64a 962 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
891be019 963 | term RELOP term /* $x > $y, etc. */
b5bbe64a 964 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
891be019 965 | term EQOP term /* $x == $y, $x eq $y */
b5bbe64a 966 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
891be019 967 | term BITANDOP term /* $x & $y */
b5bbe64a 968 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
891be019 969 | term BITOROP term /* $x | $y */
b5bbe64a 970 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
891be019 971 | term DOTDOT term /* $x..$y, $x...$y */
b5bbe64a 972 { $$ = newRANGE($2, scalar($1), scalar($3)); }
891be019 973 | term ANDAND term /* $x && $y */
b5bbe64a 974 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
891be019 975 | term OROR term /* $x || $y */
b5bbe64a 976 { $$ = newLOGOP(OP_OR, 0, $1, $3); }
c963b151 977 | term DORDOR term /* $x // $y */
b5bbe64a 978 { $$ = newLOGOP(OP_DOR, 0, $1, $3); }
891be019 979 | term MATCHOP term /* $x =~ /$y/ */
b5bbe64a 980 { $$ = bind_match($2, $1, $3); }
891be019 981 ;
8d063cd8 982
891be019
SC
983/* Unary operators and terms */
984termunop : '-' term %prec UMINUS /* -$x */
b5bbe64a 985 { $$ = newUNOP(OP_NEGATE, 0, scalar($2)); }
891be019 986 | '+' term %prec UMINUS /* +$x */
b5bbe64a
JH
987 { $$ = $2; }
988
891be019 989 | '!' term /* !$x */
b5bbe64a 990 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
891be019 991 | '~' term /* ~$x */
8823cb89 992 { $$ = newUNOP($1, 0, scalar($2)); }
891be019 993 | term POSTINC /* $x++ */
79072805 994 { $$ = newUNOP(OP_POSTINC, 0,
b5bbe64a 995 op_lvalue(scalar($1), OP_POSTINC)); }
891be019 996 | term POSTDEC /* $x-- */
79072805 997 { $$ = newUNOP(OP_POSTDEC, 0,
b5bbe64a 998 op_lvalue(scalar($1), OP_POSTDEC));}
cc624add 999 | term POSTJOIN /* implicit join after interpolated ->@ */
03d05f6e 1000 { $$ = op_convert_list(OP_JOIN, 0,
cc624add
FC
1001 op_append_elem(
1002 OP_LIST,
1003 newSVREF(scalar(
1004 newSVOP(OP_CONST,0,
1005 newSVpvs("\""))
1006 )),
1007 $1
1008 ));
cc624add 1009 }
891be019 1010 | PREINC term /* ++$x */
79072805 1011 { $$ = newUNOP(OP_PREINC, 0,
b5bbe64a 1012 op_lvalue(scalar($2), OP_PREINC)); }
891be019 1013 | PREDEC term /* --$x */
79072805 1014 { $$ = newUNOP(OP_PREDEC, 0,
b5bbe64a 1015 op_lvalue(scalar($2), OP_PREDEC)); }
891be019
SC
1016
1017 ;
1018
1019/* Constructors for anonymous data */
1020anonymous: '[' expr ']'
b5bbe64a 1021 { $$ = newANONLIST($2); }
891be019 1022 | '[' ']'
a9f5ab8d 1023 { $$ = newANONLIST(NULL);}
891be019 1024 | HASHBRACK expr ';' '}' %prec '(' /* { foo => "Bar" } */
b5bbe64a 1025 { $$ = newANONHASH($2); }
891be019 1026 | HASHBRACK ';' '}' %prec '(' /* { } (';' by tokener) */
a9f5ab8d 1027 { $$ = newANONHASH(NULL); }
894f226e 1028 | ANONSUB startanonsub proto subattrlist realsubbody %prec '('
436ddf68
DM
1029 { SvREFCNT_inc_simple_void(PL_compcv);
1030 $$ = newANONATTRSUB($2, $3, $4, $5); }
1031 | ANON_SIGSUB startanonsub proto subattrlist realsubbody %prec '('
5a5094bd 1032 { SvREFCNT_inc_simple_void(PL_compcv);
b5bbe64a 1033 $$ = newANONATTRSUB($2, $3, $4, $5); }
891be019
SC
1034 ;
1035
1036/* Things called with "do" */
1037termdo : DO term %prec UNIOP /* do $filename */
b5bbe64a 1038 { $$ = dofile($2, $1);}
891be019 1039 | DO block %prec '(' /* do { code */
b5bbe64a 1040 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, op_scope($2));}
891be019
SC
1041 ;
1042
1043term : termbinop
1044 | termunop
1045 | anonymous
1046 | termdo
e9bdcc27 1047 | term '?' term ':' term
b5bbe64a 1048 { $$ = newCONDOP(0, $1, $3, $5); }
891be019 1049 | REFGEN term /* \$x, \@y, \%z */
070d3cb6 1050 { $$ = newUNOP(OP_REFGEN, 0, $2); }
e118fea3
FC
1051 | MY REFGEN term
1052 { $$ = newUNOP(OP_REFGEN, 0, localize($3,1)); }
09bef843
SB
1053 | myattrterm %prec UNIOP
1054 { $$ = $1; }
1055 | LOCAL term %prec UNIOP
28383d1a 1056 { $$ = localize($2,0); }
a0d0e21e 1057 | '(' expr ')'
b5bbe64a 1058 { $$ = sawparens($2); }
ea25a9b2 1059 | QWLIST
b5bbe64a 1060 { $$ = $1; }
8d063cd8 1061 | '(' ')'
b5bbe64a 1062 { $$ = sawparens(newNULLLIST()); }
79072805 1063 | scalar %prec '('
8d063cd8 1064 { $$ = $1; }
79072805 1065 | star %prec '('
8d063cd8 1066 { $$ = $1; }
79072805 1067 | hsh %prec '('
8d063cd8 1068 { $$ = $1; }
79072805 1069 | ary %prec '('
8d063cd8 1070 { $$ = $1; }
891be019 1071 | arylen %prec '(' /* $#x, $#{ something } */
79072805 1072 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
fad39ff1
SM
1073 | subscripted
1074 { $$ = $1; }
89f35911 1075 | sliceme '[' expr ']' /* array slice */
2fcb4757 1076 { $$ = op_prepend_elem(OP_ASLICE,
79072805 1077 newOP(OP_PUSHMARK, 0),
79072805
LW
1078 newLISTOP(OP_ASLICE, 0,
1079 list($3),
f05e27e5 1080 ref($1, OP_ASLICE)));
429a2555
FC
1081 if ($$ && $1)
1082 $$->op_private |=
1083 $1->op_private & OPpSLICEWARNING;
f05e27e5 1084 }
76eba8ab 1085 | kvslice '[' expr ']' /* array key/value slice */
6dd3e0f2
RZ
1086 { $$ = op_prepend_elem(OP_KVASLICE,
1087 newOP(OP_PUSHMARK, 0),
1088 newLISTOP(OP_KVASLICE, 0,
1089 list($3),
1090 ref(oopsAV($1), OP_KVASLICE)));
95a31aad
FC
1091 if ($$ && $1)
1092 $$->op_private |=
1093 $1->op_private & OPpSLICEWARNING;
6dd3e0f2 1094 }
89f35911 1095 | sliceme '{' expr ';' '}' /* @hash{@keys} */
2fcb4757 1096 { $$ = op_prepend_elem(OP_HSLICE,
79072805 1097 newOP(OP_PUSHMARK, 0),
79072805
LW
1098 newLISTOP(OP_HSLICE, 0,
1099 list($3),
a0d0e21e 1100 ref(oopsHV($1), OP_HSLICE)));
429a2555
FC
1101 if ($$ && $1)
1102 $$->op_private |=
1103 $1->op_private & OPpSLICEWARNING;
f05e27e5 1104 }
76eba8ab 1105 | kvslice '{' expr ';' '}' /* %hash{@keys} */
5cae3edb
RZ
1106 { $$ = op_prepend_elem(OP_KVHSLICE,
1107 newOP(OP_PUSHMARK, 0),
1108 newLISTOP(OP_KVHSLICE, 0,
1109 list($3),
1110 ref($1, OP_KVHSLICE)));
95a31aad
FC
1111 if ($$ && $1)
1112 $$->op_private |=
1113 $1->op_private & OPpSLICEWARNING;
5cae3edb 1114 }
79072805
LW
1115 | THING %prec '('
1116 { $$ = $1; }
891be019 1117 | amper /* &foo; */
c07a80fd 1118 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
fb602e32 1119 | amper '(' ')' /* &foo() or foo() */
f05e27e5 1120 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1));
f05e27e5 1121 }
fb602e32 1122 | amper '(' expr ')' /* &foo(@args) or foo(@args) */
f05e27e5
DM
1123 {
1124 $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
2fcb4757 1125 op_append_elem(OP_LIST, $3, scalar($1)));
f05e27e5 1126 }
fb602e32 1127 | NOAMP subname optlistexpr /* foo @args (no parens) */
a0d0e21e 1128 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
2fcb4757 1129 op_append_elem(OP_LIST, $3, scalar($2)));
f05e27e5 1130 }
89f35911 1131 | term ARROW '$' '*'
b5bbe64a 1132 { $$ = newSVREF($1); }
89f35911 1133 | term ARROW '@' '*'
b5bbe64a 1134 { $$ = newAVREF($1); }
89f35911 1135 | term ARROW '%' '*'
b5bbe64a 1136 { $$ = newHVREF($1); }
89f35911
FC
1137 | term ARROW '&' '*'
1138 { $$ = newUNOP(OP_ENTERSUB, 0,
b5bbe64a 1139 scalar(newCVREF($3,$1))); }
89f35911 1140 | term ARROW '*' '*' %prec '('
b5bbe64a 1141 { $$ = newGVREF(0,$1); }
891be019 1142 | LOOPEX /* loop exiting command (goto, last, dump, etc) */
b5bbe64a
JH
1143 { $$ = newOP($1, OPf_SPECIAL);
1144 PL_hints |= HINT_BLOCK_SCOPE; }
a0d0e21e 1145 | LOOPEX term
b5bbe64a 1146 { $$ = newLOOPEX($1,$2); }
09b1cffe 1147 | NOTOP listexpr /* not $foo */
b5bbe64a 1148 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
891be019 1149 | UNIOP /* Unary op, $_ implied */
b5bbe64a 1150 { $$ = newOP($1, 0); }
f05e27e5 1151 | UNIOP block /* eval { foo }* */
b5bbe64a 1152 { $$ = newUNOP($1, 0, $2); }
891be019 1153 | UNIOP term /* Unary op */
b5bbe64a 1154 { $$ = newUNOP($1, 0, $2); }
d2fdf8fd 1155 | REQUIRE /* require, $_ implied */
b5bbe64a 1156 { $$ = newOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0); }
d2fdf8fd 1157 | REQUIRE term /* require Foo */
b5bbe64a 1158 { $$ = newUNOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0, $2); }
3cd0a11a
RGS
1159 | UNIOPSUB
1160 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
891be019 1161 | UNIOPSUB term /* Sub treated as unop */
4633a7c4 1162 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
2fcb4757 1163 op_append_elem(OP_LIST, $2, scalar($1))); }
891be019 1164 | FUNC0 /* Nullary operator */
b5bbe64a 1165 { $$ = newOP($1, 0); }
ae986130 1166 | FUNC0 '(' ')'
b5bbe64a 1167 { $$ = newOP($1, 0);}
7eb971ee
FC
1168 | FUNC0OP /* Same as above, but op created in toke.c */
1169 { $$ = $1; }
1170 | FUNC0OP '(' ')'
b5bbe64a 1171 { $$ = $1; }
891be019 1172 | FUNC0SUB /* Sub treated as nullop */
b5bbe64a 1173 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
891be019 1174 | FUNC1 '(' ')' /* not () */
b5bbe64a
JH
1175 { $$ = ($1 == OP_NOT)
1176 ? newUNOP($1, 0, newSVOP(OP_CONST, 0, newSViv(0)))
1177 : newOP($1, OPf_SPECIAL); }
891be019 1178 | FUNC1 '(' expr ')' /* not($foo) */
b5bbe64a 1179 { $$ = newUNOP($1, 0, $3); }
d63c20f2
DM
1180 | PMFUNC /* m//, s///, qr//, tr/// */
1181 {
1182 if ( $1->op_type != OP_TRANS
1183 && $1->op_type != OP_TRANSR
1184 && (((PMOP*)$1)->op_pmflags & PMf_HAS_CV))
1185 {
1186 $<ival>$ = start_subparse(FALSE, CVf_ANON);
1187 SAVEFREESV(PL_compcv);
1188 } else
1189 $<ival>$ = 0;
1190 }
9b6b7be8
FC
1191 '(' listexpr optrepl ')'
1192 { $$ = pmruntime($1, $4, $5, 1, $<ival>2); }
185c2e96 1193 | BAREWORD
378cc40b 1194 | listop
88e1f1a2 1195 | PLUGEXPR
8d063cd8
LW
1196 ;
1197
891be019 1198/* "my" declarations, with optional attributes */
09bef843 1199myattrterm: MY myterm myattrlist
b5bbe64a 1200 { $$ = my_attrs($2,$3); }
09bef843 1201 | MY myterm
28383d1a 1202 { $$ = localize($2,1); }
e118fea3
FC
1203 | MY REFGEN myterm myattrlist
1204 { $$ = newUNOP(OP_REFGEN, 0, my_attrs($3,$4)); }
09bef843
SB
1205 ;
1206
891be019 1207/* Things that can be "my"'d */
09bef843 1208myterm : '(' expr ')'
b5bbe64a 1209 { $$ = sawparens($2); }
09bef843 1210 | '(' ')'
b5bbe64a
JH
1211 { $$ = sawparens(newNULLLIST()); }
1212
09bef843
SB
1213 | scalar %prec '('
1214 { $$ = $1; }
1215 | hsh %prec '('
1216 { $$ = $1; }
1217 | ary %prec '('
1218 { $$ = $1; }
1219 ;
1220
891be019 1221/* Basic list expressions */
09b1cffe 1222optlistexpr: /* NULL */ %prec PREC_LOW
a9f5ab8d 1223 { $$ = NULL; }
09b1cffe 1224 | listexpr %prec PREC_LOW
a0d0e21e
LW
1225 { $$ = $1; }
1226 ;
1227
09b1cffe 1228optexpr: /* NULL */
a9f5ab8d 1229 { $$ = NULL; }
79072805
LW
1230 | expr
1231 { $$ = $1; }
1232 ;
1233
9b6b7be8 1234optrepl: /* NULL */
a9f5ab8d 1235 { $$ = NULL; }
9b6b7be8
FC
1236 | '/' expr
1237 { $$ = $2; }
1238 ;
1239
891be019
SC
1240/* A little bit of trickery to make "for my $foo (@bar)" actually be
1241 lexical */
55497cff 1242my_scalar: scalar
624fa8bd 1243 { parser->in_my = 0; $$ = my($1); }
55497cff
PP
1244 ;
1245
d39c26a6
FC
1246my_var : scalar
1247 | ary
1248 | hsh
1249 ;
1250
1251refgen_topic: my_var
1252 | amper
1253 ;
1254
e118fea3
FC
1255my_refgen: MY REFGEN
1256 | REFGEN MY
1257 ;
1258
79072805 1259amper : '&' indirob
b5bbe64a 1260 { $$ = newCVREF($1,$2); }
a687059c
LW
1261 ;
1262
79072805 1263scalar : '$' indirob
b5bbe64a 1264 { $$ = newSVREF($2); }
a687059c
LW
1265 ;
1266
79072805 1267ary : '@' indirob
f05e27e5 1268 { $$ = newAVREF($2);
b5bbe64a 1269 if ($$) $$->op_private |= $1;
f05e27e5 1270 }
79072805
LW
1271 ;
1272
1273hsh : '%' indirob
f05e27e5 1274 { $$ = newHVREF($2);
b5bbe64a 1275 if ($$) $$->op_private |= $1;
f05e27e5 1276 }
79072805
LW
1277 ;
1278
1279arylen : DOLSHARP indirob
b5bbe64a 1280 { $$ = newAVREF($2); }
ff25e5db 1281 | term ARROW DOLSHARP '*'
b5bbe64a 1282 { $$ = newAVREF($1); }
79072805
LW
1283 ;
1284
1285star : '*' indirob
b5bbe64a 1286 { $$ = newGVREF(0,$2); }
79072805
LW
1287 ;
1288
89f35911
FC
1289sliceme : ary
1290 | term ARROW '@'
b5bbe64a 1291 { $$ = newAVREF($1); }
89f35911
FC
1292 ;
1293
76eba8ab
FC
1294kvslice : hsh
1295 | term ARROW '%'
b5bbe64a 1296 { $$ = newHVREF($1); }
76eba8ab
FC
1297 ;
1298
89f35911
FC
1299gelem : star
1300 | term ARROW '*'
b5bbe64a 1301 { $$ = newGVREF(0,$1); }
89f35911
FC
1302 ;
1303
891be019 1304/* Indirect objects */
185c2e96 1305indirob : BAREWORD
79072805 1306 { $$ = scalar($1); }
fad39ff1 1307 | scalar %prec PREC_LOW
f05e27e5 1308 { $$ = scalar($1); }
79072805 1309 | block
3ad73efd 1310 { $$ = op_scope($1); }
79072805 1311
93a17b20
LW
1312 | PRIVATEREF
1313 { $$ = $1; }
8d063cd8 1314 ;