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