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