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