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