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