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