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