This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add postderef_qq feature feature
[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 *
f05e27e5
DM
23 * Note that these derived files are included and compiled twice; once
24 * from perly.c, and once from madly.c. The second time, a number of MAD
25 * macros are defined, which compile in extra code that allows the parse
26 * tree to be accurately dumped. In particular:
27 *
28 * MAD defined if compiling madly.c
29 * DO_MAD(A) expands to A under madly.c, to null otherwise
30 * IF_MAD(a,b) expands to A under madly.c, to B otherwise
31 * TOKEN_GETMAD() expands to token_getmad() under madly.c, to null otherwise
32 * TOKEN_FREE() similarly
33 * OP_GETMAD() similarly
34 * IVAL(i) expands to (i)->tk_lval.ival or (i)
35 * PVAL(p) expands to (p)->tk_lval.pval or (p)
36 *
166f8a29
DM
37 * The main job of of this grammar is to call the various newFOO()
38 * functions in op.c to build a syntax tree of OP structs.
61296642 39 * It relies on the lexer in toke.c to do the tokenizing.
29522234
DM
40 *
41 * Note: due to the way that the cleanup code works WRT to freeing ops on
42 * the parse stack, it is dangerous to assign to the $n variables within
43 * an action.
166f8a29
DM
44 */
45
0de566d7 46/* Make the parser re-entrant. */
8d063cd8 47
0de566d7 48%pure_parser
8d063cd8 49
f05e27e5
DM
50/* FIXME for MAD - is the new mintro on while and until important? */
51
28ac2b49 52%start grammar
9d116dd7 53
8d063cd8 54%union {
d5c6462e
DM
55 I32 ival; /* __DEFAULT__ (marker for regen_perly.pl;
56 must always be 1st union member) */
79072805
LW
57 char *pval;
58 OP *opval;
59 GV *gvval;
f05e27e5
DM
60#ifdef PERL_IN_MADLY_C
61 TOKEN* p_tkval;
123d08c9 62 TOKEN* i_tkval;
f05e27e5
DM
63#else
64 char *p_tkval;
123d08c9
DM
65 I32 i_tkval;
66#endif
67#ifdef PERL_MAD
68 TOKEN* tkval;
f05e27e5 69#endif
8d063cd8
LW
70}
71
78cdf107 72%token <ival> GRAMPROG GRAMEXPR GRAMBLOCK GRAMBARESTMT GRAMFULLSTMT GRAMSTMTSEQ
28ac2b49 73
7c70caa5 74%token <i_tkval> '{' '}' '[' ']' '-' '+' '$' '@' '%' '*' '&' ';' '=' '.'
f0fcb552 75
ea25a9b2 76%token <opval> WORD METHOD FUNCMETH THING PMFUNC PRIVATEREF QWLIST
7eb971ee 77%token <opval> FUNC0OP FUNC0SUB UNIOPSUB LSTOPSUB
88e1f1a2 78%token <opval> PLUGEXPR PLUGSTMT
f05e27e5 79%token <p_tkval> LABEL
123d08c9
DM
80%token <i_tkval> FORMAT SUB ANONSUB PACKAGE USE
81%token <i_tkval> WHILE UNTIL IF UNLESS ELSE ELSIF CONTINUE FOR
82%token <i_tkval> GIVEN WHEN DEFAULT
be25f609 83%token <i_tkval> LOOPEX DOTDOT YADAYADA
123d08c9
DM
84%token <i_tkval> FUNC0 FUNC1 FUNC UNIOP LSTOP
85%token <i_tkval> RELOP EQOP MULOP ADDOP
86%token <i_tkval> DOLSHARP DO HASHBRACK NOAMP
8d2a5fb4 87%token <i_tkval> LOCAL MY REQUIRE
705fe0e5 88%token <i_tkval> COLONATTR FORMLBRACK FORMRBRACK
f05e27e5 89
727a8fe5 90%type <ival> grammar remember mremember
f05e27e5
DM
91%type <ival> startsub startanonsub startformsub
92/* FIXME for MAD - are these two ival? */
b5a64814 93%type <ival> mintro
f05e27e5 94
8e720305 95%type <opval> stmtseq fullstmt labfullstmt barestmt block mblock else
fad39ff1 96%type <opval> expr term subscripted scalar ary hsh arylen star amper sideff
76eba8ab 97%type <opval> sliceme kvslice gelem
09b1cffe
Z
98%type <opval> listexpr nexpr texpr iexpr mexpr mnexpr miexpr
99%type <opval> optlistexpr optexpr indirob listop method
7c70caa5 100%type <opval> formname subname proto subbody cont my_scalar formblock
f05e27e5 101%type <opval> subattrlist myattrlist myattrterm myterm
891be019 102%type <opval> termbinop termunop anonymous termdo
705fe0e5 103%type <opval> formstmtseq formline formarg
79072805 104
123d08c9 105%nonassoc <i_tkval> PREC_LOW
fad39ff1
SM
106%nonassoc LOOPEX
107
123d08c9
DM
108%left <i_tkval> OROP DOROP
109%left <i_tkval> ANDOP
110%right <i_tkval> NOTOP
36477c24 111%nonassoc LSTOP LSTOPSUB
123d08c9
DM
112%left <i_tkval> ','
113%right <i_tkval> ASSIGNOP
114%right <i_tkval> '?' ':'
be25f609 115%nonassoc DOTDOT YADAYADA
123d08c9
DM
116%left <i_tkval> OROR DORDOR
117%left <i_tkval> ANDAND
118%left <i_tkval> BITOROP
119%left <i_tkval> BITANDOP
a687059c
LW
120%nonassoc EQOP
121%nonassoc RELOP
36477c24 122%nonassoc UNIOP UNIOPSUB
20515881 123%nonassoc REQUIRE
123d08c9 124%left <i_tkval> SHIFTOP
a687059c
LW
125%left ADDOP
126%left MULOP
123d08c9
DM
127%left <i_tkval> MATCHOP
128%right <i_tkval> '!' '~' UMINUS REFGEN
129%right <i_tkval> POWOP
130%nonassoc <i_tkval> PREINC PREDEC POSTINC POSTDEC
131%left <i_tkval> ARROW
132%nonassoc <i_tkval> ')'
133%left <i_tkval> '('
fad39ff1 134%left '[' '{'
8d063cd8 135
123d08c9 136%token <i_tkval> PEG
7f46837f 137
8d063cd8
LW
138%% /* RULES */
139
28ac2b49 140/* Top-level choice of what kind of thing yyparse was called to parse */
727a8fe5
Z
141grammar : GRAMPROG
142 {
143 PL_parser->expect = XSTATE;
144 }
145 remember stmtseq
146 {
147 newPROG(block_end($3,$4));
148 $$ = 0;
149 }
78cdf107
Z
150 | GRAMEXPR
151 {
152 parser->expect = XTERM;
153 }
09b1cffe 154 optexpr
78cdf107
Z
155 {
156 PL_eval_root = $3;
157 $$ = 0;
158 }
e53d8f76
Z
159 | GRAMBLOCK
160 {
161 parser->expect = XBLOCK;
162 }
163 block
164 {
5f211341 165 PL_pad_reset_pending = TRUE;
8359b381
Z
166 PL_eval_root = $3;
167 $$ = 0;
168 yyunlex();
169 parser->yychar = YYEOF;
170 }
171 | GRAMBARESTMT
172 {
173 parser->expect = XSTATE;
174 }
175 barestmt
176 {
177 PL_pad_reset_pending = TRUE;
e53d8f76
Z
178 PL_eval_root = $3;
179 $$ = 0;
180 yyunlex();
181 parser->yychar = YYEOF;
182 }
9eb5c532 183 | GRAMFULLSTMT
28ac2b49 184 {
9eb5c532
Z
185 parser->expect = XSTATE;
186 }
187 fullstmt
188 {
5f211341 189 PL_pad_reset_pending = TRUE;
9eb5c532 190 PL_eval_root = $3;
28ac2b49
Z
191 $$ = 0;
192 yyunlex();
193 parser->yychar = YYEOF;
194 }
07ffcb73
Z
195 | GRAMSTMTSEQ
196 {
197 parser->expect = XSTATE;
198 }
5f211341 199 stmtseq
07ffcb73
Z
200 {
201 PL_eval_root = $3;
202 $$ = 0;
203 }
204 ;
205
891be019 206/* An ordinary block */
5f211341 207block : '{' remember stmtseq '}'
53a7735b
DM
208 { if (PL_parser->copline > (line_t)IVAL($1))
209 PL_parser->copline = (line_t)IVAL($1);
f05e27e5
DM
210 $$ = block_end($2, $3);
211 TOKEN_GETMAD($1,$$,'{');
212 TOKEN_GETMAD($4,$$,'}');
213 }
a0d0e21e
LW
214 ;
215
7c70caa5 216/* format body */
705fe0e5 217formblock: '=' remember ';' FORMRBRACK formstmtseq ';' '.'
7c70caa5
FC
218 { if (PL_parser->copline > (line_t)IVAL($1))
219 PL_parser->copline = (line_t)IVAL($1);
705fe0e5 220 $$ = block_end($2, $5);
7c70caa5 221 TOKEN_GETMAD($1,$$,'{');
705fe0e5 222 TOKEN_GETMAD($7,$$,'}');
7c70caa5
FC
223 }
224 ;
225
55497cff
PP
226remember: /* NULL */ /* start a full lexical scope */
227 { $$ = block_start(TRUE); }
228 ;
229
5f211341 230mblock : '{' mremember stmtseq '}'
53a7735b
DM
231 { if (PL_parser->copline > (line_t)IVAL($1))
232 PL_parser->copline = (line_t)IVAL($1);
f05e27e5
DM
233 $$ = block_end($2, $3);
234 TOKEN_GETMAD($1,$$,'{');
235 TOKEN_GETMAD($4,$$,'}');
236 }
55497cff
PP
237 ;
238
239mremember: /* NULL */ /* start a partial lexical scope */
240 { $$ = block_start(FALSE); }
8d063cd8
LW
241 ;
242
eae48c89 243/* A sequence of statements in the program */
5f211341 244stmtseq : /* NULL */
bcabcc50 245 { $$ = (OP*)NULL; }
5f211341 246 | stmtseq fullstmt
2fcb4757 247 { $$ = op_append_list(OP_LINESEQ, $1, $2);
3280af22 248 PL_pad_reset_pending = TRUE;
503de470
DM
249 if ($1 && $2)
250 PL_hints |= HINT_BLOCK_SCOPE;
251 }
8d063cd8
LW
252 ;
253
705fe0e5
FC
254/* A sequence of format lines */
255formstmtseq: /* NULL */
256 { $$ = (OP*)NULL; }
257 | formstmtseq formline
258 { $$ = op_append_list(OP_LINESEQ, $1, $2);
259 PL_pad_reset_pending = TRUE;
260 if ($1 && $2)
261 PL_hints |= HINT_BLOCK_SCOPE;
262 }
263 ;
264
8e720305
Z
265/* A statement in the program, including optional labels */
266fullstmt: barestmt
eae48c89 267 {
8e720305
Z
268 if($1) {
269 $$ = newSTATEOP(0, NULL, $1);
eae48c89 270 } else {
8e720305 271 $$ = IF_MAD(newOP(OP_NULL, 0), NULL);
eae48c89
Z
272 }
273 }
8e720305
Z
274 | labfullstmt
275 { $$ = $1; }
276 ;
277
278labfullstmt: LABEL barestmt
279 {
5504e6cf
FC
280 $$ = newSTATEOP(SVf_UTF8
281 * PVAL($1)[strlen(PVAL($1))+1],
282 PVAL($1), $2);
8e720305
Z
283 TOKEN_GETMAD($1,
284 $2 ? cLISTOPx($$)->op_first : $$, 'L');
285 }
286 | LABEL labfullstmt
287 {
5504e6cf
FC
288 $$ = newSTATEOP(SVf_UTF8
289 * PVAL($1)[strlen(PVAL($1))+1],
290 PVAL($1), $2);
8e720305
Z
291 TOKEN_GETMAD($1, cLISTOPx($$)->op_first, 'L');
292 }
eae48c89
Z
293 ;
294
295/* A bare statement, lacking label and other aspects of state op */
296barestmt: PLUGSTMT
0d863452 297 { $$ = $1; }
eae48c89
Z
298 | PEG
299 {
300 $$ = newOP(OP_NULL,0);
301 TOKEN_GETMAD($1,$$,'p');
302 }
7c70caa5 303 | FORMAT startformsub formname formblock
f05e27e5 304 {
eae48c89 305 CV *fmtcv = PL_compcv;
eae48c89
Z
306#ifdef MAD
307 $$ = newFORM($2, $3, $4);
308 prepend_madprops($1->tk_mad, $$, 'F');
309 $1->tk_mad = 0;
310 token_free($1);
311#else
312 newFORM($2, $3, $4);
313 $$ = (OP*)NULL;
314#endif
4a273b91 315 if (CvOUTSIDE(fmtcv) && !CvEVAL(CvOUTSIDE(fmtcv))) {
eae48c89 316 SvREFCNT_inc_simple_void(fmtcv);
cc76b5cc 317 pad_add_anon(fmtcv, OP_NULL);
f05e27e5 318 }
eae48c89 319 }
50278755
FC
320 | SUB subname startsub
321 {
322 if ($2->op_type == OP_CONST) {
323 const char *const name =
324 SvPV_nolen_const(((SVOP*)$2)->op_sv);
325 if (strEQ(name, "BEGIN") || strEQ(name, "END")
764212cf
FC
326 || strEQ(name, "INIT") || strEQ(name, "CHECK")
327 || strEQ(name, "UNITCHECK"))
328 CvSPECIAL_on(PL_compcv);
50278755 329 }
e07561e6
FC
330 else
331 /* State subs inside anonymous subs need to be
332 clonable themselves. */
6d5c2147
FC
333 if (CvANON(CvOUTSIDE(PL_compcv))
334 || CvCLONE(CvOUTSIDE(PL_compcv))
335 || !PadnameIsSTATE(PadlistNAMESARRAY(CvPADLIST(
336 CvOUTSIDE(PL_compcv)
337 ))[$2->op_targ]))
e07561e6 338 CvCLONE_on(PL_compcv);
764212cf
FC
339 PL_parser->in_my = 0;
340 PL_parser->in_my_stash = NULL;
341 }
342 proto subattrlist subbody
eae48c89
Z
343 {
344 SvREFCNT_inc_simple_void(PL_compcv);
345#ifdef MAD
346 {
347 OP* o = newSVOP(OP_ANONCODE, 0,
50278755
FC
348 (SV*)(
349#endif
350 $2->op_type == OP_CONST
351 ? newATTRSUB($3, $2, $5, $6, $7)
352 : newMYSUB($3, $2, $5, $6, $7)
353#ifdef MAD
354 ));
eae48c89
Z
355 $$ = newOP(OP_NULL,0);
356 op_getmad(o,$$,'&');
764212cf
FC
357 op_getmad($2,$$,'n');
358 op_getmad($5,$$,'s');
359 op_getmad($6,$$,'a');
eae48c89 360 token_getmad($1,$$,'d');
764212cf
FC
361 append_madprops($7->op_madprop, $$, 0);
362 $7->op_madprop = 0;
f05e27e5 363 }
eae48c89 364#else
50278755 365 ;
eae48c89
Z
366 $$ = (OP*)NULL;
367#endif
764212cf 368 intro_my();
f05e27e5 369 }
eae48c89 370 | PACKAGE WORD WORD ';'
5f211341 371 {
eae48c89
Z
372#ifdef MAD
373 $$ = package($3);
374 token_getmad($1,$$,'o');
375 if ($2)
376 package_version($2);
377 token_getmad($4,$$,';');
378#else
379 package($3);
380 if ($2)
381 package_version($2);
382 $$ = (OP*)NULL;
383#endif
384 }
385 | USE startsub
386 { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
09b1cffe 387 WORD WORD optlistexpr ';'
eae48c89
Z
388 {
389 SvREFCNT_inc_simple_void(PL_compcv);
390#ifdef MAD
391 $$ = utilize(IVAL($1), $2, $4, $5, $6);
392 token_getmad($1,$$,'o');
393 token_getmad($7,$$,';');
394 if (PL_parser->rsfp_filters &&
395 AvFILLp(PL_parser->rsfp_filters) >= 0)
396 append_madprops(newMADPROP('!', MAD_NULL, NULL, 0), $$, 0);
397#else
398 utilize(IVAL($1), $2, $4, $5, $6);
399 $$ = (OP*)NULL;
400#endif
401 }
417a992d 402 | IF '(' remember mexpr ')' mblock else
eae48c89 403 {
3ad73efd
Z
404 $$ = block_end($3,
405 newCONDOP(0, $4, op_scope($6), $7));
eae48c89
Z
406 TOKEN_GETMAD($1,$$,'I');
407 TOKEN_GETMAD($2,$$,'(');
408 TOKEN_GETMAD($5,$$,')');
409 PL_parser->copline = (line_t)IVAL($1);
410 }
417a992d 411 | UNLESS '(' remember miexpr ')' mblock else
eae48c89 412 {
3ad73efd
Z
413 $$ = block_end($3,
414 newCONDOP(0, $4, op_scope($6), $7));
eae48c89
Z
415 TOKEN_GETMAD($1,$$,'I');
416 TOKEN_GETMAD($2,$$,'(');
417 TOKEN_GETMAD($5,$$,')');
418 PL_parser->copline = (line_t)IVAL($1);
419 }
b5a64814 420 | GIVEN '(' remember mexpr ')' mblock
eae48c89 421 {
b5a64814 422 const PADOFFSET offset = pad_findmy_pvs("$_", 0);
eae48c89 423 $$ = block_end($3,
b5a64814
FC
424 newGIVENOP($4, op_scope($6),
425 offset == NOT_IN_PAD
426 || PAD_COMPNAME_FLAGS_isOUR(offset)
427 ? 0
428 : offset));
eae48c89
Z
429 PL_parser->copline = (line_t)IVAL($1);
430 }
417a992d 431 | WHEN '(' remember mexpr ')' mblock
3ad73efd 432 { $$ = block_end($3, newWHENOP($4, op_scope($6))); }
eae48c89 433 | DEFAULT block
3ad73efd 434 { $$ = newWHENOP(0, op_scope($2)); }
417a992d 435 | WHILE '(' remember texpr ')' mintro mblock cont
eae48c89
Z
436 {
437 $$ = block_end($3,
438 newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
94bf0465 439 $4, $7, $8, $6));
eae48c89
Z
440 TOKEN_GETMAD($1,$$,'W');
441 TOKEN_GETMAD($2,$$,'(');
442 TOKEN_GETMAD($5,$$,')');
443 PL_parser->copline = (line_t)IVAL($1);
444 }
417a992d 445 | UNTIL '(' remember iexpr ')' mintro mblock cont
eae48c89
Z
446 {
447 $$ = block_end($3,
94bf0465
Z
448 newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
449 $4, $7, $8, $6));
eae48c89
Z
450 TOKEN_GETMAD($1,$$,'W');
451 TOKEN_GETMAD($2,$$,'(');
452 TOKEN_GETMAD($5,$$,')');
453 PL_parser->copline = (line_t)IVAL($1);
454 }
417a992d 455 | FOR '(' remember mnexpr ';' texpr ';' mintro mnexpr ')'
eae48c89
Z
456 mblock
457 {
458 OP *initop = IF_MAD($4 ? $4 : newOP(OP_NULL, 0), $4);
459 OP *forop = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
94bf0465 460 scalar($6), $11, $9, $8);
eae48c89
Z
461 if (initop) {
462 forop = op_prepend_elem(OP_LINESEQ, initop,
463 op_append_elem(OP_LINESEQ,
464 newOP(OP_UNSTACK, OPf_SPECIAL),
465 forop));
5f211341 466 }
eae48c89
Z
467 DO_MAD({ forop = newUNOP(OP_NULL, 0, forop); })
468 $$ = block_end($3, forop);
469 TOKEN_GETMAD($1,$$,'3');
470 TOKEN_GETMAD($2,$$,'(');
471 TOKEN_GETMAD($5,$$,'1');
472 TOKEN_GETMAD($7,$$,'2');
473 TOKEN_GETMAD($10,$$,')');
474 PL_parser->copline = (line_t)IVAL($1);
475 }
417a992d 476 | FOR MY remember my_scalar '(' mexpr ')' mblock cont
eae48c89 477 {
94bf0465 478 $$ = block_end($3, newFOROP(0, $4, $6, $8, $9));
eae48c89
Z
479 TOKEN_GETMAD($1,$$,'W');
480 TOKEN_GETMAD($2,$$,'d');
481 TOKEN_GETMAD($5,$$,'(');
482 TOKEN_GETMAD($7,$$,')');
483 PL_parser->copline = (line_t)IVAL($1);
484 }
417a992d 485 | FOR scalar '(' remember mexpr ')' mblock cont
eae48c89 486 {
94bf0465 487 $$ = block_end($4, newFOROP(0,
3ad73efd 488 op_lvalue($2, OP_ENTERLOOP), $5, $7, $8));
eae48c89
Z
489 TOKEN_GETMAD($1,$$,'W');
490 TOKEN_GETMAD($3,$$,'(');
491 TOKEN_GETMAD($6,$$,')');
492 PL_parser->copline = (line_t)IVAL($1);
493 }
417a992d 494 | FOR '(' remember mexpr ')' mblock cont
eae48c89
Z
495 {
496 $$ = block_end($3,
94bf0465 497 newFOROP(0, (OP*)NULL, $4, $6, $7));
eae48c89
Z
498 TOKEN_GETMAD($1,$$,'W');
499 TOKEN_GETMAD($2,$$,'(');
500 TOKEN_GETMAD($5,$$,')');
501 PL_parser->copline = (line_t)IVAL($1);
502 }
503 | block cont
504 {
505 /* a block is a loop that happens once */
506 $$ = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
94bf0465 507 (OP*)NULL, $1, $2, 0);
eae48c89
Z
508 }
509 | PACKAGE WORD WORD '{' remember
510 {
eae48c89 511 package($3);
eae48c89 512 if ($2) {
eae48c89 513 package_version($2);
eae48c89
Z
514 }
515 }
516 stmtseq '}'
517 {
518 /* a block is a loop that happens once */
94bf0465 519 $$ = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
eae48c89 520 (OP*)NULL, block_end($5, $7), (OP*)NULL, 0);
eae48c89
Z
521 TOKEN_GETMAD($4,$$,'{');
522 TOKEN_GETMAD($8,$$,'}');
523 if (PL_parser->copline > (line_t)IVAL($4))
524 PL_parser->copline = (line_t)IVAL($4);
525 }
526 | sideff ';'
527 {
528 PL_parser->expect = XSTATE;
529 $$ = $1;
530 TOKEN_GETMAD($2,$$,';');
531 }
532 | ';'
533 {
534 PL_parser->expect = XSTATE;
535 $$ = IF_MAD(newOP(OP_NULL, 0), (OP*)NULL);
536 TOKEN_GETMAD($1,$$,';');
537 PL_parser->copline = NOLINE;
5f211341 538 }
8d063cd8
LW
539 ;
540
705fe0e5
FC
541/* Format line */
542formline: THING formarg
543 { OP *list;
544 if ($2) {
545 OP *term = $2;
546 DO_MAD(term = newUNOP(OP_NULL, 0, term));
547 list = op_append_elem(OP_LIST, $1, term);
548 }
549 else {
550#ifdef MAD
551 OP *op = newNULLLIST();
552 list = op_append_elem(OP_LIST, $1, op);
553#else
554 list = $1;
555#endif
556 }
557 if (PL_parser->copline == NOLINE)
558 PL_parser->copline = CopLINE(PL_curcop)-1;
559 else PL_parser->copline--;
560 $$ = newSTATEOP(0, NULL,
561 convert(OP_FORMLINE, 0, list));
562 }
563 ;
564
565formarg : /* NULL */
566 { $$ = NULL; }
567 | FORMLBRACK stmtseq FORMRBRACK
568 { $$ = op_unscope($2); }
569 ;
570
891be019 571/* An expression which may have a side-effect */
a687059c 572sideff : error
bcabcc50 573 { $$ = (OP*)NULL; }
a687059c 574 | expr
79072805 575 { $$ = $1; }
a687059c 576 | expr IF expr
f05e27e5
DM
577 { $$ = newLOGOP(OP_AND, 0, $3, $1);
578 TOKEN_GETMAD($2,$$,'i');
579 }
a687059c 580 | expr UNLESS expr
f05e27e5
DM
581 { $$ = newLOGOP(OP_OR, 0, $3, $1);
582 TOKEN_GETMAD($2,$$,'i');
583 }
a687059c 584 | expr WHILE expr
f05e27e5
DM
585 { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1);
586 TOKEN_GETMAD($2,$$,'w');
587 }
55497cff 588 | expr UNTIL iexpr
f05e27e5
DM
589 { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1);
590 TOKEN_GETMAD($2,$$,'w');
591 }
ecca16b0 592 | expr FOR expr
94bf0465 593 { $$ = newFOROP(0, (OP*)NULL, $3, $1, (OP*)NULL);
eae48c89 594 TOKEN_GETMAD($2,$$,'w');
94bf0465 595 PL_parser->copline = (line_t)IVAL($2);
f05e27e5 596 }
f20dcd76 597 | expr WHEN expr
3ad73efd 598 { $$ = newWHENOP($3, op_scope($1)); }
79072805
LW
599 ;
600
891be019 601/* else and elsif blocks */
79072805 602else : /* NULL */
bcabcc50 603 { $$ = (OP*)NULL; }
55497cff 604 | ELSE mblock
3ad73efd
Z
605 {
606 ($2)->op_flags |= OPf_PARENS;
607 $$ = op_scope($2);
f05e27e5
DM
608 TOKEN_GETMAD($1,$$,'o');
609 }
417a992d 610 | ELSIF '(' mexpr ')' mblock else
53a7735b 611 { PL_parser->copline = (line_t)IVAL($1);
3ad73efd
Z
612 $$ = newCONDOP(0,
613 newSTATEOP(OPf_SPECIAL,NULL,$3),
614 op_scope($5), $6);
615 PL_hints |= HINT_BLOCK_SCOPE;
f05e27e5
DM
616 TOKEN_GETMAD($1,$$,'I');
617 TOKEN_GETMAD($2,$$,'(');
618 TOKEN_GETMAD($4,$$,')');
619 }
79072805
LW
620 ;
621
891be019 622/* Continue blocks */
79072805 623cont : /* NULL */
bcabcc50 624 { $$ = (OP*)NULL; }
79072805 625 | CONTINUE block
3ad73efd
Z
626 {
627 $$ = op_scope($2);
f05e27e5
DM
628 TOKEN_GETMAD($1,$$,'o');
629 }
79072805
LW
630 ;
631
a034e688
DM
632/* determine whether there are any new my declarations */
633mintro : /* NULL */
634 { $$ = (PL_min_intro_pending &&
635 PL_max_intro_pending >= PL_min_intro_pending);
636 intro_my(); }
637
891be019 638/* Normal expression */
8d063cd8 639nexpr : /* NULL */
bcabcc50 640 { $$ = (OP*)NULL; }
8d063cd8
LW
641 | sideff
642 ;
643
891be019 644/* Boolean expression */
8d063cd8 645texpr : /* NULL means true */
f05e27e5
DM
646 { YYSTYPE tmplval;
647 (void)scan_num("1", &tmplval);
648 $$ = tmplval.opval; }
8d063cd8
LW
649 | expr
650 ;
651
891be019 652/* Inverted boolean expression */
55497cff
PP
653iexpr : expr
654 { $$ = invert(scalar($1)); }
655 ;
656
891be019 657/* Expression with its own lexical scope */
55497cff 658mexpr : expr
bbce6d69
PP
659 { $$ = $1; intro_my(); }
660 ;
661
662mnexpr : nexpr
663 { $$ = $1; intro_my(); }
55497cff
PP
664 ;
665
55497cff 666miexpr : iexpr
bbce6d69 667 { $$ = $1; intro_my(); }
55497cff
PP
668 ;
669
44a8e56a 670formname: WORD { $$ = $1; }
bcabcc50 671 | /* NULL */ { $$ = (OP*)NULL; }
8d063cd8
LW
672 ;
673
fa83b5b6 674startsub: /* NULL */ /* start a regular subroutine scope */
a8ff2fa6
DM
675 { $$ = start_subparse(FALSE, 0);
676 SAVEFREESV(PL_compcv); }
f05e27e5 677
28757baa
PP
678 ;
679
680startanonsub: /* NULL */ /* start an anonymous subroutine scope */
a8ff2fa6
DM
681 { $$ = start_subparse(FALSE, CVf_ANON);
682 SAVEFREESV(PL_compcv); }
28757baa
PP
683 ;
684
44a8e56a 685startformsub: /* NULL */ /* start a format subroutine scope */
a8ff2fa6
DM
686 { $$ = start_subparse(TRUE, 0);
687 SAVEFREESV(PL_compcv); }
44a8e56a
PP
688 ;
689
891be019 690/* Name of a subroutine - must be a bareword, could be special */
50278755 691subname : WORD
4210d3f1 692 | PRIVATEREF
a0d0e21e
LW
693 ;
694
891be019 695/* Subroutine prototype */
4633a7c4 696proto : /* NULL */
bcabcc50 697 { $$ = (OP*)NULL; }
4633a7c4
LW
698 | THING
699 ;
28757baa 700
891be019 701/* Optional list of subroutine attributes */
09bef843 702subattrlist: /* NULL */
bcabcc50 703 { $$ = (OP*)NULL; }
09bef843 704 | COLONATTR THING
f05e27e5
DM
705 { $$ = $2;
706 TOKEN_GETMAD($1,$$,':');
707 }
09bef843 708 | COLONATTR
f05e27e5
DM
709 { $$ = IF_MAD(
710 newOP(OP_NULL, 0),
bcabcc50 711 (OP*)NULL
f05e27e5
DM
712 );
713 TOKEN_GETMAD($1,$$,':');
714 }
09bef843
SB
715 ;
716
891be019 717/* List of attributes for a "my" variable declaration */
09bef843 718myattrlist: COLONATTR THING
f05e27e5
DM
719 { $$ = $2;
720 TOKEN_GETMAD($1,$$,':');
721 }
09bef843 722 | COLONATTR
f05e27e5
DM
723 { $$ = IF_MAD(
724 newOP(OP_NULL, 0),
bcabcc50 725 (OP*)NULL
f05e27e5
DM
726 );
727 TOKEN_GETMAD($1,$$,':');
728 }
09bef843
SB
729 ;
730
891be019 731/* Subroutine body - either null or a block */
28757baa 732subbody : block { $$ = $1; }
f05e27e5
DM
733 | ';' { $$ = IF_MAD(
734 newOP(OP_NULL,0),
bcabcc50 735 (OP*)NULL
f05e27e5 736 );
53a7735b 737 PL_parser->expect = XSTATE;
f05e27e5
DM
738 TOKEN_GETMAD($1,$$,';');
739 }
8d063cd8
LW
740 ;
741
891be019 742/* Ordinary expressions; logical combinations */
a0d0e21e 743expr : expr ANDOP expr
f05e27e5
DM
744 { $$ = newLOGOP(OP_AND, 0, $1, $3);
745 TOKEN_GETMAD($2,$$,'o');
746 }
a0d0e21e 747 | expr OROP expr
f05e27e5
DM
748 { $$ = newLOGOP(IVAL($2), 0, $1, $3);
749 TOKEN_GETMAD($2,$$,'o');
750 }
c963b151 751 | expr DOROP expr
f05e27e5
DM
752 { $$ = newLOGOP(OP_DOR, 0, $1, $3);
753 TOKEN_GETMAD($2,$$,'o');
754 }
09b1cffe 755 | listexpr %prec PREC_LOW
a0d0e21e
LW
756 ;
757
891be019 758/* Expressions are a list of terms joined by commas */
09b1cffe 759listexpr: listexpr ','
f05e27e5
DM
760 {
761#ifdef MAD
762 OP* op = newNULLLIST();
763 token_getmad($2,op,',');
2fcb4757 764 $$ = op_append_elem(OP_LIST, $1, op);
f05e27e5
DM
765#else
766 $$ = $1;
767#endif
768 }
09b1cffe 769 | listexpr ',' term
f05e27e5 770 {
29522234 771 OP* term = $3;
f05e27e5 772 DO_MAD(
29522234
DM
773 term = newUNOP(OP_NULL, 0, term);
774 token_getmad($2,term,',');
f05e27e5 775 )
2fcb4757 776 $$ = op_append_elem(OP_LIST, $1, term);
f05e27e5 777 }
fad39ff1 778 | term %prec PREC_LOW
8d063cd8
LW
779 ;
780
891be019 781/* List operators */
09b1cffe 782listop : LSTOP indirob listexpr /* map {...} @args or print $fh @args */
f05e27e5 783 { $$ = convert(IVAL($1), OPf_STACKED,
2fcb4757 784 op_prepend_elem(OP_LIST, newGVREF(IVAL($1),$2), $3) );
f05e27e5
DM
785 TOKEN_GETMAD($1,$$,'o');
786 }
891be019 787 | FUNC '(' indirob expr ')' /* print ($fh @args */
f05e27e5 788 { $$ = convert(IVAL($1), OPf_STACKED,
2fcb4757 789 op_prepend_elem(OP_LIST, newGVREF(IVAL($1),$3), $4) );
f05e27e5
DM
790 TOKEN_GETMAD($1,$$,'o');
791 TOKEN_GETMAD($2,$$,'(');
792 TOKEN_GETMAD($5,$$,')');
793 }
417a992d 794 | term ARROW method '(' optexpr ')' /* $foo->bar(list) */
4633a7c4 795 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
2fcb4757
Z
796 op_append_elem(OP_LIST,
797 op_prepend_elem(OP_LIST, scalar($1), $5),
f05e27e5
DM
798 newUNOP(OP_METHOD, 0, $3)));
799 TOKEN_GETMAD($2,$$,'A');
800 TOKEN_GETMAD($4,$$,'(');
801 TOKEN_GETMAD($6,$$,')');
802 }
891be019 803 | term ARROW method /* $foo->bar */
b1524f17 804 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
2fcb4757 805 op_append_elem(OP_LIST, scalar($1),
f05e27e5
DM
806 newUNOP(OP_METHOD, 0, $3)));
807 TOKEN_GETMAD($2,$$,'A');
808 }
09b1cffe 809 | METHOD indirob optlistexpr /* new Class @args */
4633a7c4 810 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
2fcb4757
Z
811 op_append_elem(OP_LIST,
812 op_prepend_elem(OP_LIST, $2, $3),
f05e27e5
DM
813 newUNOP(OP_METHOD, 0, $1)));
814 }
09b1cffe 815 | FUNCMETH indirob '(' optexpr ')' /* method $object (@args) */
4633a7c4 816 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
2fcb4757
Z
817 op_append_elem(OP_LIST,
818 op_prepend_elem(OP_LIST, $2, $4),
f05e27e5
DM
819 newUNOP(OP_METHOD, 0, $1)));
820 TOKEN_GETMAD($3,$$,'(');
821 TOKEN_GETMAD($5,$$,')');
822 }
09b1cffe 823 | LSTOP optlistexpr /* print @args */
f05e27e5
DM
824 { $$ = convert(IVAL($1), 0, $2);
825 TOKEN_GETMAD($1,$$,'o');
826 }
09b1cffe 827 | FUNC '(' optexpr ')' /* print (@args) */
f05e27e5
DM
828 { $$ = convert(IVAL($1), 0, $3);
829 TOKEN_GETMAD($1,$$,'o');
830 TOKEN_GETMAD($2,$$,'(');
831 TOKEN_GETMAD($4,$$,')');
832 }
718a7425 833 | LSTOPSUB startanonsub block /* sub f(&@); f { foo } ... */
5a5094bd 834 { SvREFCNT_inc_simple_void(PL_compcv);
bcabcc50 835 $<opval>$ = newANONATTRSUB($2, 0, (OP*)NULL, $3); }
09b1cffe 836 optlistexpr %prec LSTOP /* ... @bar */
4633a7c4 837 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
2fcb4757
Z
838 op_append_elem(OP_LIST,
839 op_prepend_elem(OP_LIST, $<opval>4, $5), $1));
f05e27e5 840 }
a687059c
LW
841 ;
842
891be019 843/* Names of methods. May use $object->$methodname */
a0d0e21e
LW
844method : METHOD
845 | scalar
846 ;
847
891be019 848/* Some kind of subscripted expression */
89f35911 849subscripted: gelem '{' expr ';' '}' /* *main::{something} */
891be019
SC
850 /* In this and all the hash accessors, ';' is
851 * provided by the tokeniser */
264e1af3 852 { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3));
53a7735b 853 PL_parser->expect = XOPERATOR;
f05e27e5
DM
854 TOKEN_GETMAD($2,$$,'{');
855 TOKEN_GETMAD($4,$$,';');
856 TOKEN_GETMAD($5,$$,'}');
857 }
891be019 858 | scalar '[' expr ']' /* $array[$element] */
f05e27e5
DM
859 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3));
860 TOKEN_GETMAD($2,$$,'[');
861 TOKEN_GETMAD($4,$$,']');
862 }
891be019 863 | term ARROW '[' expr ']' /* somearef->[$element] */
fad39ff1
SM
864 { $$ = newBINOP(OP_AELEM, 0,
865 ref(newAVREF($1),OP_RV2AV),
f05e27e5
DM
866 scalar($4));
867 TOKEN_GETMAD($2,$$,'a');
868 TOKEN_GETMAD($3,$$,'[');
869 TOKEN_GETMAD($5,$$,']');
870 }
891be019 871 | subscripted '[' expr ']' /* $foo->[$bar]->[$baz] */
fad39ff1
SM
872 { $$ = newBINOP(OP_AELEM, 0,
873 ref(newAVREF($1),OP_RV2AV),
f05e27e5
DM
874 scalar($3));
875 TOKEN_GETMAD($2,$$,'[');
876 TOKEN_GETMAD($4,$$,']');
877 }
5a63ceef 878 | scalar '{' expr ';' '}' /* $foo{bar();} */
fad39ff1 879 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
53a7735b 880 PL_parser->expect = XOPERATOR;
f05e27e5
DM
881 TOKEN_GETMAD($2,$$,'{');
882 TOKEN_GETMAD($4,$$,';');
883 TOKEN_GETMAD($5,$$,'}');
884 }
891be019 885 | term ARROW '{' expr ';' '}' /* somehref->{bar();} */
fad39ff1
SM
886 { $$ = newBINOP(OP_HELEM, 0,
887 ref(newHVREF($1),OP_RV2HV),
888 jmaybe($4));
53a7735b 889 PL_parser->expect = XOPERATOR;
f05e27e5
DM
890 TOKEN_GETMAD($2,$$,'a');
891 TOKEN_GETMAD($3,$$,'{');
892 TOKEN_GETMAD($5,$$,';');
893 TOKEN_GETMAD($6,$$,'}');
894 }
891be019 895 | subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */
fad39ff1
SM
896 { $$ = newBINOP(OP_HELEM, 0,
897 ref(newHVREF($1),OP_RV2HV),
898 jmaybe($3));
53a7735b 899 PL_parser->expect = XOPERATOR;
f05e27e5
DM
900 TOKEN_GETMAD($2,$$,'{');
901 TOKEN_GETMAD($4,$$,';');
902 TOKEN_GETMAD($5,$$,'}');
903 }
891be019 904 | term ARROW '(' ')' /* $subref->() */
fad39ff1 905 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
f05e27e5
DM
906 newCVREF(0, scalar($1)));
907 TOKEN_GETMAD($2,$$,'a');
908 TOKEN_GETMAD($3,$$,'(');
909 TOKEN_GETMAD($4,$$,')');
910 }
891be019 911 | term ARROW '(' expr ')' /* $subref->(@args) */
fad39ff1 912 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
2fcb4757 913 op_append_elem(OP_LIST, $4,
f05e27e5
DM
914 newCVREF(0, scalar($1))));
915 TOKEN_GETMAD($2,$$,'a');
916 TOKEN_GETMAD($3,$$,'(');
917 TOKEN_GETMAD($5,$$,')');
918 }
fad39ff1 919
417a992d 920 | subscripted '(' expr ')' /* $foo->{bar}->(@args) */
fad39ff1 921 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
2fcb4757 922 op_append_elem(OP_LIST, $3,
f05e27e5
DM
923 newCVREF(0, scalar($1))));
924 TOKEN_GETMAD($2,$$,'(');
925 TOKEN_GETMAD($4,$$,')');
926 }
417a992d 927 | subscripted '(' ')' /* $foo->{bar}->() */
fad39ff1 928 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
f05e27e5
DM
929 newCVREF(0, scalar($1)));
930 TOKEN_GETMAD($2,$$,'(');
931 TOKEN_GETMAD($3,$$,')');
932 }
9a9798c2 933 | '(' expr ')' '[' expr ']' /* list slice */
f05e27e5
DM
934 { $$ = newSLICEOP(0, $5, $2);
935 TOKEN_GETMAD($1,$$,'(');
936 TOKEN_GETMAD($3,$$,')');
937 TOKEN_GETMAD($4,$$,'[');
938 TOKEN_GETMAD($6,$$,']');
939 }
ea25a9b2
Z
940 | QWLIST '[' expr ']' /* list literal slice */
941 { $$ = newSLICEOP(0, $3, $1);
942 TOKEN_GETMAD($2,$$,'[');
943 TOKEN_GETMAD($4,$$,']');
944 }
9a9798c2 945 | '(' ')' '[' expr ']' /* empty list slice! */
bcabcc50 946 { $$ = newSLICEOP(0, $4, (OP*)NULL);
f05e27e5
DM
947 TOKEN_GETMAD($1,$$,'(');
948 TOKEN_GETMAD($2,$$,')');
949 TOKEN_GETMAD($3,$$,'[');
950 TOKEN_GETMAD($5,$$,']');
951 }
891be019 952 ;
fad39ff1 953
891be019 954/* Binary operators between terms */
f05e27e5
DM
955termbinop: term ASSIGNOP term /* $x = $y */
956 { $$ = newASSIGNOP(OPf_STACKED, $1, IVAL($2), $3);
957 TOKEN_GETMAD($2,$$,'o');
958 }
891be019 959 | term POWOP term /* $x ** $y */
f05e27e5
DM
960 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
961 TOKEN_GETMAD($2,$$,'o');
962 }
891be019 963 | term MULOP term /* $x * $y, $x x $y */
f05e27e5 964 { if (IVAL($2) != OP_REPEAT)
79072805 965 scalar($1);
f05e27e5
DM
966 $$ = newBINOP(IVAL($2), 0, $1, scalar($3));
967 TOKEN_GETMAD($2,$$,'o');
968 }
891be019 969 | term ADDOP term /* $x + $y */
f05e27e5
DM
970 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
971 TOKEN_GETMAD($2,$$,'o');
972 }
891be019 973 | term SHIFTOP term /* $x >> $y, $x << $y */
f05e27e5
DM
974 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
975 TOKEN_GETMAD($2,$$,'o');
976 }
891be019 977 | term RELOP term /* $x > $y, etc. */
f05e27e5
DM
978 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
979 TOKEN_GETMAD($2,$$,'o');
980 }
891be019 981 | term EQOP term /* $x == $y, $x eq $y */
f05e27e5
DM
982 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
983 TOKEN_GETMAD($2,$$,'o');
984 }
891be019 985 | term BITANDOP term /* $x & $y */
f05e27e5
DM
986 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
987 TOKEN_GETMAD($2,$$,'o');
988 }
891be019 989 | term BITOROP term /* $x | $y */
f05e27e5
DM
990 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
991 TOKEN_GETMAD($2,$$,'o');
992 }
891be019 993 | term DOTDOT term /* $x..$y, $x...$y */
f05e27e5
DM
994 {
995 $$ = newRANGE(IVAL($2), scalar($1), scalar($3));
035e2bcc 996 DO_MAD({
f05e27e5
DM
997 UNOP *op;
998 op = (UNOP*)$$;
999 op = (UNOP*)op->op_first; /* get to flop */
1000 op = (UNOP*)op->op_first; /* get to flip */
1001 op = (UNOP*)op->op_first; /* get to range */
1002 token_getmad($2,(OP*)op,'o');
04ff073f 1003 });
f05e27e5 1004 }
891be019 1005 | term ANDAND term /* $x && $y */
f05e27e5
DM
1006 { $$ = newLOGOP(OP_AND, 0, $1, $3);
1007 TOKEN_GETMAD($2,$$,'o');
1008 }
891be019 1009 | term OROR term /* $x || $y */
f05e27e5
DM
1010 { $$ = newLOGOP(OP_OR, 0, $1, $3);
1011 TOKEN_GETMAD($2,$$,'o');
1012 }
c963b151 1013 | term DORDOR term /* $x // $y */
f05e27e5
DM
1014 { $$ = newLOGOP(OP_DOR, 0, $1, $3);
1015 TOKEN_GETMAD($2,$$,'o');
1016 }
891be019 1017 | term MATCHOP term /* $x =~ /$y/ */
f05e27e5
DM
1018 { $$ = bind_match(IVAL($2), $1, $3);
1019 TOKEN_GETMAD($2,
1020 ($$->op_type == OP_NOT
1021 ? ((UNOP*)$$)->op_first : $$),
1022 '~');
1023 }
891be019 1024 ;
8d063cd8 1025
891be019
SC
1026/* Unary operators and terms */
1027termunop : '-' term %prec UMINUS /* -$x */
f05e27e5
DM
1028 { $$ = newUNOP(OP_NEGATE, 0, scalar($2));
1029 TOKEN_GETMAD($1,$$,'o');
1030 }
891be019 1031 | '+' term %prec UMINUS /* +$x */
f05e27e5
DM
1032 { $$ = IF_MAD(
1033 newUNOP(OP_NULL, 0, $2),
1034 $2
1035 );
1036 TOKEN_GETMAD($1,$$,'+');
1037 }
891be019 1038 | '!' term /* !$x */
f05e27e5
DM
1039 { $$ = newUNOP(OP_NOT, 0, scalar($2));
1040 TOKEN_GETMAD($1,$$,'o');
1041 }
891be019 1042 | '~' term /* ~$x */
f05e27e5
DM
1043 { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));
1044 TOKEN_GETMAD($1,$$,'o');
1045 }
891be019 1046 | term POSTINC /* $x++ */
79072805 1047 { $$ = newUNOP(OP_POSTINC, 0,
3ad73efd 1048 op_lvalue(scalar($1), OP_POSTINC));
f05e27e5
DM
1049 TOKEN_GETMAD($2,$$,'o');
1050 }
891be019 1051 | term POSTDEC /* $x-- */
79072805 1052 { $$ = newUNOP(OP_POSTDEC, 0,
3ad73efd 1053 op_lvalue(scalar($1), OP_POSTDEC));
f05e27e5
DM
1054 TOKEN_GETMAD($2,$$,'o');
1055 }
891be019 1056 | PREINC term /* ++$x */
79072805 1057 { $$ = newUNOP(OP_PREINC, 0,
3ad73efd 1058 op_lvalue(scalar($2), OP_PREINC));
f05e27e5
DM
1059 TOKEN_GETMAD($1,$$,'o');
1060 }
891be019 1061 | PREDEC term /* --$x */
79072805 1062 { $$ = newUNOP(OP_PREDEC, 0,
3ad73efd 1063 op_lvalue(scalar($2), OP_PREDEC));
f05e27e5
DM
1064 TOKEN_GETMAD($1,$$,'o');
1065 }
891be019
SC
1066
1067 ;
1068
1069/* Constructors for anonymous data */
1070anonymous: '[' expr ']'
f05e27e5
DM
1071 { $$ = newANONLIST($2);
1072 TOKEN_GETMAD($1,$$,'[');
1073 TOKEN_GETMAD($3,$$,']');
1074 }
891be019 1075 | '[' ']'
bcabcc50 1076 { $$ = newANONLIST((OP*)NULL);
f05e27e5
DM
1077 TOKEN_GETMAD($1,$$,'[');
1078 TOKEN_GETMAD($2,$$,']');
1079 }
891be019 1080 | HASHBRACK expr ';' '}' %prec '(' /* { foo => "Bar" } */
f05e27e5
DM
1081 { $$ = newANONHASH($2);
1082 TOKEN_GETMAD($1,$$,'{');
1083 TOKEN_GETMAD($3,$$,';');
1084 TOKEN_GETMAD($4,$$,'}');
1085 }
891be019 1086 | HASHBRACK ';' '}' %prec '(' /* { } (';' by tokener) */
bcabcc50 1087 { $$ = newANONHASH((OP*)NULL);
f05e27e5
DM
1088 TOKEN_GETMAD($1,$$,'{');
1089 TOKEN_GETMAD($2,$$,';');
1090 TOKEN_GETMAD($3,$$,'}');
1091 }
718a7425 1092 | ANONSUB startanonsub proto subattrlist block %prec '('
5a5094bd 1093 { SvREFCNT_inc_simple_void(PL_compcv);
718a7425 1094 $$ = newANONATTRSUB($2, $3, $4, $5);
f05e27e5 1095 TOKEN_GETMAD($1,$$,'o');
718a7425
DM
1096 OP_GETMAD($3,$$,'s');
1097 OP_GETMAD($4,$$,'a');
f05e27e5 1098 }
891be019
SC
1099
1100 ;
1101
1102/* Things called with "do" */
1103termdo : DO term %prec UNIOP /* do $filename */
123d08c9 1104 { $$ = dofile($2, IVAL($1));
f05e27e5
DM
1105 TOKEN_GETMAD($1,$$,'o');
1106 }
891be019 1107 | DO block %prec '(' /* do { code */
3ad73efd 1108 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, op_scope($2));
f05e27e5
DM
1109 TOKEN_GETMAD($1,$$,'D');
1110 }
73f3e228 1111 | DO subname '(' ')' /* do somesub() */
891be019
SC
1112 { $$ = newUNOP(OP_ENTERSUB,
1113 OPf_SPECIAL|OPf_STACKED,
2fcb4757 1114 op_prepend_elem(OP_LIST,
891be019
SC
1115 scalar(newCVREF(
1116 (OPpENTERSUB_AMPER<<8),
1117 scalar($2)
bcabcc50 1118 )),(OP*)NULL)); dep();
f05e27e5
DM
1119 TOKEN_GETMAD($1,$$,'o');
1120 TOKEN_GETMAD($3,$$,'(');
1121 TOKEN_GETMAD($4,$$,')');
1122 }
73f3e228 1123 | DO subname '(' expr ')' /* do somesub(@args) */
891be019
SC
1124 { $$ = newUNOP(OP_ENTERSUB,
1125 OPf_SPECIAL|OPf_STACKED,
2fcb4757 1126 op_append_elem(OP_LIST,
891be019
SC
1127 $4,
1128 scalar(newCVREF(
1129 (OPpENTERSUB_AMPER<<8),
1130 scalar($2)
f05e27e5
DM
1131 )))); dep();
1132 TOKEN_GETMAD($1,$$,'o');
1133 TOKEN_GETMAD($3,$$,'(');
1134 TOKEN_GETMAD($5,$$,')');
1135 }
417a992d 1136 | DO scalar '(' ')' /* do $subref () */
891be019 1137 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
2fcb4757 1138 op_prepend_elem(OP_LIST,
bcabcc50 1139 scalar(newCVREF(0,scalar($2))), (OP*)NULL)); dep();
f05e27e5
DM
1140 TOKEN_GETMAD($1,$$,'o');
1141 TOKEN_GETMAD($3,$$,'(');
1142 TOKEN_GETMAD($4,$$,')');
1143 }
417a992d 1144 | DO scalar '(' expr ')' /* do $subref (@args) */
891be019 1145 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
2fcb4757 1146 op_prepend_elem(OP_LIST,
891be019 1147 $4,
f05e27e5
DM
1148 scalar(newCVREF(0,scalar($2))))); dep();
1149 TOKEN_GETMAD($1,$$,'o');
1150 TOKEN_GETMAD($3,$$,'(');
1151 TOKEN_GETMAD($5,$$,')');
1152 }
891be019
SC
1153
1154 ;
1155
1156term : termbinop
1157 | termunop
1158 | anonymous
1159 | termdo
e9bdcc27 1160 | term '?' term ':' term
f05e27e5
DM
1161 { $$ = newCONDOP(0, $1, $3, $5);
1162 TOKEN_GETMAD($2,$$,'?');
1163 TOKEN_GETMAD($4,$$,':');
1164 }
891be019 1165 | REFGEN term /* \$x, \@y, \%z */
3ad73efd 1166 { $$ = newUNOP(OP_REFGEN, 0, op_lvalue($2,OP_REFGEN));
f05e27e5
DM
1167 TOKEN_GETMAD($1,$$,'o');
1168 }
09bef843
SB
1169 | myattrterm %prec UNIOP
1170 { $$ = $1; }
1171 | LOCAL term %prec UNIOP
f05e27e5 1172 { $$ = localize($2,IVAL($1));
dde83331 1173 TOKEN_GETMAD($1,$$,'k');
f05e27e5 1174 }
a0d0e21e 1175 | '(' expr ')'
f05e27e5
DM
1176 { $$ = sawparens(IF_MAD(newUNOP(OP_NULL,0,$2), $2));
1177 TOKEN_GETMAD($1,$$,'(');
1178 TOKEN_GETMAD($3,$$,')');
1179 }
ea25a9b2
Z
1180 | QWLIST
1181 { $$ = IF_MAD(newUNOP(OP_NULL,0,$1), $1); }
8d063cd8 1182 | '(' ')'
f05e27e5
DM
1183 { $$ = sawparens(newNULLLIST());
1184 TOKEN_GETMAD($1,$$,'(');
1185 TOKEN_GETMAD($2,$$,')');
1186 }
79072805 1187 | scalar %prec '('
8d063cd8 1188 { $$ = $1; }
79072805 1189 | star %prec '('
8d063cd8 1190 { $$ = $1; }
79072805 1191 | hsh %prec '('
8d063cd8 1192 { $$ = $1; }
79072805 1193 | ary %prec '('
8d063cd8 1194 { $$ = $1; }
891be019 1195 | arylen %prec '(' /* $#x, $#{ something } */
79072805 1196 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
fad39ff1
SM
1197 | subscripted
1198 { $$ = $1; }
89f35911 1199 | sliceme '[' expr ']' /* array slice */
2fcb4757 1200 { $$ = op_prepend_elem(OP_ASLICE,
79072805 1201 newOP(OP_PUSHMARK, 0),
79072805
LW
1202 newLISTOP(OP_ASLICE, 0,
1203 list($3),
f05e27e5 1204 ref($1, OP_ASLICE)));
429a2555
FC
1205 if ($$ && $1)
1206 $$->op_private |=
1207 $1->op_private & OPpSLICEWARNING;
f05e27e5
DM
1208 TOKEN_GETMAD($2,$$,'[');
1209 TOKEN_GETMAD($4,$$,']');
1210 }
76eba8ab 1211 | kvslice '[' expr ']' /* array key/value slice */
6dd3e0f2
RZ
1212 { $$ = op_prepend_elem(OP_KVASLICE,
1213 newOP(OP_PUSHMARK, 0),
1214 newLISTOP(OP_KVASLICE, 0,
1215 list($3),
1216 ref(oopsAV($1), OP_KVASLICE)));
95a31aad
FC
1217 if ($$ && $1)
1218 $$->op_private |=
1219 $1->op_private & OPpSLICEWARNING;
6dd3e0f2
RZ
1220 TOKEN_GETMAD($2,$$,'[');
1221 TOKEN_GETMAD($4,$$,']');
1222 }
89f35911 1223 | sliceme '{' expr ';' '}' /* @hash{@keys} */
2fcb4757 1224 { $$ = op_prepend_elem(OP_HSLICE,
79072805 1225 newOP(OP_PUSHMARK, 0),
79072805
LW
1226 newLISTOP(OP_HSLICE, 0,
1227 list($3),
a0d0e21e 1228 ref(oopsHV($1), OP_HSLICE)));
429a2555
FC
1229 if ($$ && $1)
1230 $$->op_private |=
1231 $1->op_private & OPpSLICEWARNING;
53a7735b 1232 PL_parser->expect = XOPERATOR;
f05e27e5
DM
1233 TOKEN_GETMAD($2,$$,'{');
1234 TOKEN_GETMAD($4,$$,';');
1235 TOKEN_GETMAD($5,$$,'}');
1236 }
76eba8ab 1237 | kvslice '{' expr ';' '}' /* %hash{@keys} */
5cae3edb
RZ
1238 { $$ = op_prepend_elem(OP_KVHSLICE,
1239 newOP(OP_PUSHMARK, 0),
1240 newLISTOP(OP_KVHSLICE, 0,
1241 list($3),
1242 ref($1, OP_KVHSLICE)));
95a31aad
FC
1243 if ($$ && $1)
1244 $$->op_private |=
1245 $1->op_private & OPpSLICEWARNING;
5cae3edb
RZ
1246 PL_parser->expect = XOPERATOR;
1247 TOKEN_GETMAD($2,$$,'{');
1248 TOKEN_GETMAD($4,$$,';');
1249 TOKEN_GETMAD($5,$$,'}');
1250 }
79072805
LW
1251 | THING %prec '('
1252 { $$ = $1; }
891be019 1253 | amper /* &foo; */
c07a80fd 1254 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
fb602e32 1255 | amper '(' ')' /* &foo() or foo() */
f05e27e5
DM
1256 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1));
1257 TOKEN_GETMAD($2,$$,'(');
1258 TOKEN_GETMAD($3,$$,')');
1259 }
fb602e32 1260 | amper '(' expr ')' /* &foo(@args) or foo(@args) */
f05e27e5
DM
1261 {
1262 $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
2fcb4757 1263 op_append_elem(OP_LIST, $3, scalar($1)));
035e2bcc 1264 DO_MAD({
f05e27e5
DM
1265 OP* op = $$;
1266 if (op->op_type == OP_CONST) { /* defeat const fold */
1267 op = (OP*)op->op_madprop->mad_val;
1268 }
1269 token_getmad($2,op,'(');
1270 token_getmad($4,op,')');
04ff073f 1271 });
f05e27e5 1272 }
fb602e32 1273 | NOAMP subname optlistexpr /* foo @args (no parens) */
a0d0e21e 1274 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
2fcb4757 1275 op_append_elem(OP_LIST, $3, scalar($2)));
f05e27e5
DM
1276 TOKEN_GETMAD($1,$$,'o');
1277 }
89f35911
FC
1278 | term ARROW '$' '*'
1279 { $$ = newSVREF($1);
1280 TOKEN_GETMAD($3,$$,'$');
1281 }
1282 | term ARROW '@' '*'
1283 { $$ = newAVREF($1);
1284 TOKEN_GETMAD($3,$$,'@');
1285 }
1286 | term ARROW '%' '*'
1287 { $$ = newHVREF($1);
1288 TOKEN_GETMAD($3,$$,'%');
1289 }
1290 | term ARROW '&' '*'
1291 { $$ = newUNOP(OP_ENTERSUB, 0,
1292 scalar(newCVREF(IVAL($3),$1)));
1293 TOKEN_GETMAD($3,$$,'&');
1294 }
1295 | term ARROW '*' '*' %prec '('
1296 { $$ = newGVREF(0,$1);
1297 TOKEN_GETMAD($3,$$,'*');
1298 }
891be019 1299 | LOOPEX /* loop exiting command (goto, last, dump, etc) */
f05e27e5
DM
1300 { $$ = newOP(IVAL($1), OPf_SPECIAL);
1301 PL_hints |= HINT_BLOCK_SCOPE;
1302 TOKEN_GETMAD($1,$$,'o');
1303 }
a0d0e21e 1304 | LOOPEX term
f05e27e5
DM
1305 { $$ = newLOOPEX(IVAL($1),$2);
1306 TOKEN_GETMAD($1,$$,'o');
1307 }
09b1cffe 1308 | NOTOP listexpr /* not $foo */
f05e27e5
DM
1309 { $$ = newUNOP(OP_NOT, 0, scalar($2));
1310 TOKEN_GETMAD($1,$$,'o');
1311 }
891be019 1312 | UNIOP /* Unary op, $_ implied */
f05e27e5
DM
1313 { $$ = newOP(IVAL($1), 0);
1314 TOKEN_GETMAD($1,$$,'o');
1315 }
1316 | UNIOP block /* eval { foo }* */
1317 { $$ = newUNOP(IVAL($1), 0, $2);
1318 TOKEN_GETMAD($1,$$,'o');
1319 }
891be019 1320 | UNIOP term /* Unary op */
f05e27e5
DM
1321 { $$ = newUNOP(IVAL($1), 0, $2);
1322 TOKEN_GETMAD($1,$$,'o');
1323 }
d2fdf8fd
RGS
1324 | REQUIRE /* require, $_ implied */
1325 { $$ = newOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0);
1326 TOKEN_GETMAD($1,$$,'o');
1327 }
1328 | REQUIRE term /* require Foo */
1329 { $$ = newUNOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0, $2);
1330 TOKEN_GETMAD($1,$$,'o');
1331 }
3cd0a11a
RGS
1332 | UNIOPSUB
1333 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
891be019 1334 | UNIOPSUB term /* Sub treated as unop */
4633a7c4 1335 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
2fcb4757 1336 op_append_elem(OP_LIST, $2, scalar($1))); }
891be019 1337 | FUNC0 /* Nullary operator */
f05e27e5
DM
1338 { $$ = newOP(IVAL($1), 0);
1339 TOKEN_GETMAD($1,$$,'o');
1340 }
ae986130 1341 | FUNC0 '(' ')'
f05e27e5
DM
1342 { $$ = newOP(IVAL($1), 0);
1343 TOKEN_GETMAD($1,$$,'o');
1344 TOKEN_GETMAD($2,$$,'(');
1345 TOKEN_GETMAD($3,$$,')');
1346 }
7eb971ee
FC
1347 | FUNC0OP /* Same as above, but op created in toke.c */
1348 { $$ = $1; }
1349 | FUNC0OP '(' ')'
1350 { $$ = $1;
1351 TOKEN_GETMAD($2,$$,'(');
1352 TOKEN_GETMAD($3,$$,')');
1353 }
891be019 1354 | FUNC0SUB /* Sub treated as nullop */
28757baa 1355 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
4633a7c4 1356 scalar($1)); }
891be019 1357 | FUNC1 '(' ')' /* not () */
f05e27e5
DM
1358 { $$ = (IVAL($1) == OP_NOT)
1359 ? newUNOP(IVAL($1), 0, newSVOP(OP_CONST, 0, newSViv(0)))
1360 : newOP(IVAL($1), OPf_SPECIAL);
1361
1362 TOKEN_GETMAD($1,$$,'o');
1363 TOKEN_GETMAD($2,$$,'(');
1364 TOKEN_GETMAD($3,$$,')');
1365 }
891be019 1366 | FUNC1 '(' expr ')' /* not($foo) */
f05e27e5
DM
1367 { $$ = newUNOP(IVAL($1), 0, $3);
1368 TOKEN_GETMAD($1,$$,'o');
1369 TOKEN_GETMAD($2,$$,'(');
1370 TOKEN_GETMAD($4,$$,')');
1371 }
d63c20f2
DM
1372 | PMFUNC /* m//, s///, qr//, tr/// */
1373 {
1374 if ( $1->op_type != OP_TRANS
1375 && $1->op_type != OP_TRANSR
1376 && (((PMOP*)$1)->op_pmflags & PMf_HAS_CV))
1377 {
1378 $<ival>$ = start_subparse(FALSE, CVf_ANON);
1379 SAVEFREESV(PL_compcv);
1380 } else
1381 $<ival>$ = 0;
1382 }
1383 '(' listexpr ')'
1384 { $$ = pmruntime($1, $4, 1, $<ival>2);
1385 TOKEN_GETMAD($3,$$,'(');
1386 TOKEN_GETMAD($5,$$,')');
f05e27e5 1387 }
79072805 1388 | WORD
378cc40b 1389 | listop
be25f609 1390 | YADAYADA
1391 {
1392 $$ = newLISTOP(OP_DIE, 0, newOP(OP_PUSHMARK, 0),
1393 newSVOP(OP_CONST, 0, newSVpvs("Unimplemented")));
950f2ea2 1394 TOKEN_GETMAD($1,$$,'X');
be25f609 1395 }
88e1f1a2 1396 | PLUGEXPR
8d063cd8
LW
1397 ;
1398
891be019 1399/* "my" declarations, with optional attributes */
09bef843 1400myattrterm: MY myterm myattrlist
f05e27e5
DM
1401 { $$ = my_attrs($2,$3);
1402 DO_MAD(
1403 token_getmad($1,$$,'d');
1404 append_madprops($3->op_madprop, $$, 'a');
1405 $3->op_madprop = 0;
04ff073f 1406 );
f05e27e5 1407 }
09bef843 1408 | MY myterm
f05e27e5
DM
1409 { $$ = localize($2,IVAL($1));
1410 TOKEN_GETMAD($1,$$,'d');
1411 }
09bef843
SB
1412 ;
1413
891be019 1414/* Things that can be "my"'d */
09bef843 1415myterm : '(' expr ')'
f05e27e5
DM
1416 { $$ = sawparens($2);
1417 TOKEN_GETMAD($1,$$,'(');
1418 TOKEN_GETMAD($3,$$,')');
1419 }
09bef843 1420 | '(' ')'
f05e27e5
DM
1421 { $$ = sawparens(newNULLLIST());
1422 TOKEN_GETMAD($1,$$,'(');
1423 TOKEN_GETMAD($2,$$,')');
1424 }
09bef843
SB
1425 | scalar %prec '('
1426 { $$ = $1; }
1427 | hsh %prec '('
1428 { $$ = $1; }
1429 | ary %prec '('
1430 { $$ = $1; }
1431 ;
1432
891be019 1433/* Basic list expressions */
09b1cffe 1434optlistexpr: /* NULL */ %prec PREC_LOW
bcabcc50 1435 { $$ = (OP*)NULL; }
09b1cffe 1436 | listexpr %prec PREC_LOW
a0d0e21e
LW
1437 { $$ = $1; }
1438 ;
1439
09b1cffe 1440optexpr: /* NULL */
bcabcc50 1441 { $$ = (OP*)NULL; }
79072805
LW
1442 | expr
1443 { $$ = $1; }
1444 ;
1445
891be019
SC
1446/* A little bit of trickery to make "for my $foo (@bar)" actually be
1447 lexical */
55497cff 1448my_scalar: scalar
12bd6ede 1449 { PL_parser->in_my = 0; $$ = my($1); }
55497cff
PP
1450 ;
1451
79072805 1452amper : '&' indirob
f05e27e5
DM
1453 { $$ = newCVREF(IVAL($1),$2);
1454 TOKEN_GETMAD($1,$$,'&');
1455 }
a687059c
LW
1456 ;
1457
79072805 1458scalar : '$' indirob
f05e27e5
DM
1459 { $$ = newSVREF($2);
1460 TOKEN_GETMAD($1,$$,'$');
1461 }
a687059c
LW
1462 ;
1463
79072805 1464ary : '@' indirob
f05e27e5 1465 { $$ = newAVREF($2);
429a2555 1466 if ($$) $$->op_private |= IVAL($1);
f05e27e5
DM
1467 TOKEN_GETMAD($1,$$,'@');
1468 }
79072805
LW
1469 ;
1470
1471hsh : '%' indirob
f05e27e5 1472 { $$ = newHVREF($2);
95a31aad 1473 if ($$) $$->op_private |= IVAL($1);
f05e27e5
DM
1474 TOKEN_GETMAD($1,$$,'%');
1475 }
79072805
LW
1476 ;
1477
1478arylen : DOLSHARP indirob
f05e27e5
DM
1479 { $$ = newAVREF($2);
1480 TOKEN_GETMAD($1,$$,'l');
1481 }
79072805
LW
1482 ;
1483
1484star : '*' indirob
f05e27e5
DM
1485 { $$ = newGVREF(0,$2);
1486 TOKEN_GETMAD($1,$$,'*');
1487 }
79072805
LW
1488 ;
1489
89f35911
FC
1490sliceme : ary
1491 | term ARROW '@'
1492 { $$ = newAVREF($1);
1493 TOKEN_GETMAD($3,$$,'@');
1494 }
1495 ;
1496
76eba8ab
FC
1497kvslice : hsh
1498 | term ARROW '%'
1499 { $$ = newHVREF($1);
1500 TOKEN_GETMAD($3,$$,'@');
1501 }
1502 ;
1503
89f35911
FC
1504gelem : star
1505 | term ARROW '*'
1506 { $$ = newGVREF(0,$1);
1507 TOKEN_GETMAD($3,$$,'*');
1508 }
1509 ;
1510
891be019 1511/* Indirect objects */
79072805
LW
1512indirob : WORD
1513 { $$ = scalar($1); }
fad39ff1 1514 | scalar %prec PREC_LOW
f05e27e5 1515 { $$ = scalar($1); }
79072805 1516 | block
3ad73efd 1517 { $$ = op_scope($1); }
79072805 1518
93a17b20
LW
1519 | PRIVATEREF
1520 { $$ = $1; }
8d063cd8 1521 ;