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