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