This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Block signals during fork (fixes RT#82580)
[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
123d08c9 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
87%token <i_tkval> LOCAL MY MYSUB REQUIRE
88%token <i_tkval> COLONATTR
f05e27e5 89
ea25a9b2
Z
90%type <i_tkval> lpar_or_qw
91
727a8fe5 92%type <ival> grammar remember mremember
f05e27e5
DM
93%type <ival> startsub startanonsub startformsub
94/* FIXME for MAD - are these two ival? */
95%type <ival> mydefsv mintro
96
8e720305 97%type <opval> stmtseq fullstmt labfullstmt barestmt block mblock else
fad39ff1 98%type <opval> expr term subscripted scalar ary hsh arylen star amper sideff
09b1cffe
Z
99%type <opval> listexpr nexpr texpr iexpr mexpr mnexpr miexpr
100%type <opval> optlistexpr optexpr indirob listop method
44a8e56a 101%type <opval> formname subname proto subbody cont my_scalar
f05e27e5 102%type <opval> subattrlist myattrlist myattrterm myterm
891be019 103%type <opval> termbinop termunop anonymous termdo
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
55497cff
PP
216remember: /* NULL */ /* start a full lexical scope */
217 { $$ = block_start(TRUE); }
218 ;
219
0d863452 220mydefsv: /* NULL */ /* lexicalize $_ */
d6447115 221 { $$ = (I32) Perl_allocmy(aTHX_ STR_WITH_LEN("$_"), 0); }
0d863452
RH
222 ;
223
5f211341 224mblock : '{' mremember stmtseq '}'
53a7735b
DM
225 { if (PL_parser->copline > (line_t)IVAL($1))
226 PL_parser->copline = (line_t)IVAL($1);
f05e27e5
DM
227 $$ = block_end($2, $3);
228 TOKEN_GETMAD($1,$$,'{');
229 TOKEN_GETMAD($4,$$,'}');
230 }
55497cff
PP
231 ;
232
233mremember: /* NULL */ /* start a partial lexical scope */
234 { $$ = block_start(FALSE); }
8d063cd8
LW
235 ;
236
eae48c89 237/* A sequence of statements in the program */
5f211341 238stmtseq : /* NULL */
bcabcc50 239 { $$ = (OP*)NULL; }
5f211341 240 | stmtseq fullstmt
2fcb4757 241 { $$ = op_append_list(OP_LINESEQ, $1, $2);
3280af22 242 PL_pad_reset_pending = TRUE;
503de470
DM
243 if ($1 && $2)
244 PL_hints |= HINT_BLOCK_SCOPE;
245 }
8d063cd8
LW
246 ;
247
8e720305
Z
248/* A statement in the program, including optional labels */
249fullstmt: barestmt
eae48c89 250 {
8e720305
Z
251 if($1) {
252 $$ = newSTATEOP(0, NULL, $1);
eae48c89 253 } else {
8e720305 254 $$ = IF_MAD(newOP(OP_NULL, 0), NULL);
eae48c89
Z
255 }
256 }
8e720305
Z
257 | labfullstmt
258 { $$ = $1; }
259 ;
260
261labfullstmt: LABEL barestmt
262 {
5db1eb8d
BF
263 $$ = newSTATEOP(SvUTF8(((SVOP*)$1)->op_sv),
264 savepv(SvPVX(((SVOP*)$1)->op_sv)), $2);
8e720305
Z
265 TOKEN_GETMAD($1,
266 $2 ? cLISTOPx($$)->op_first : $$, 'L');
267 }
268 | LABEL labfullstmt
269 {
5db1eb8d
BF
270 $$ = newSTATEOP(SvUTF8(((SVOP*)$1)->op_sv),
271 savepv(SvPVX(((SVOP*)$1)->op_sv)), $2);
8e720305
Z
272 TOKEN_GETMAD($1, cLISTOPx($$)->op_first, 'L');
273 }
eae48c89
Z
274 ;
275
276/* A bare statement, lacking label and other aspects of state op */
277barestmt: PLUGSTMT
0d863452 278 { $$ = $1; }
eae48c89
Z
279 | PEG
280 {
281 $$ = newOP(OP_NULL,0);
282 TOKEN_GETMAD($1,$$,'p');
283 }
284 | FORMAT startformsub formname block
f05e27e5 285 {
eae48c89
Z
286 CV *fmtcv = PL_compcv;
287 SvREFCNT_inc_simple_void(PL_compcv);
288#ifdef MAD
289 $$ = newFORM($2, $3, $4);
290 prepend_madprops($1->tk_mad, $$, 'F');
291 $1->tk_mad = 0;
292 token_free($1);
293#else
294 newFORM($2, $3, $4);
295 $$ = (OP*)NULL;
296#endif
297 if (CvOUTSIDE(fmtcv) && !CvUNIQUE(CvOUTSIDE(fmtcv))) {
298 SvREFCNT_inc_simple_void(fmtcv);
cc76b5cc 299 pad_add_anon(fmtcv, OP_NULL);
f05e27e5 300 }
eae48c89
Z
301 }
302 | SUB startsub subname proto subattrlist subbody
303 {
304 SvREFCNT_inc_simple_void(PL_compcv);
305#ifdef MAD
306 {
307 OP* o = newSVOP(OP_ANONCODE, 0,
308 (SV*)newATTRSUB($2, $3, $4, $5, $6));
309 $$ = newOP(OP_NULL,0);
310 op_getmad(o,$$,'&');
311 op_getmad($3,$$,'n');
312 op_getmad($4,$$,'s');
313 op_getmad($5,$$,'a');
314 token_getmad($1,$$,'d');
315 append_madprops($6->op_madprop, $$, 0);
316 $6->op_madprop = 0;
f05e27e5 317 }
eae48c89
Z
318#else
319 newATTRSUB($2, $3, $4, $5, $6);
320 $$ = (OP*)NULL;
321#endif
f05e27e5 322 }
eae48c89 323 | MYSUB startsub subname proto subattrlist subbody
f05e27e5 324 {
eae48c89
Z
325 /* Unimplemented "my sub foo { }" */
326 SvREFCNT_inc_simple_void(PL_compcv);
327#ifdef MAD
328 $$ = newMYSUB($2, $3, $4, $5, $6);
329 token_getmad($1,$$,'d');
330#else
331 newMYSUB($2, $3, $4, $5, $6);
332 $$ = (OP*)NULL;
333#endif
334 }
335 | PACKAGE WORD WORD ';'
5f211341 336 {
eae48c89
Z
337#ifdef MAD
338 $$ = package($3);
339 token_getmad($1,$$,'o');
340 if ($2)
341 package_version($2);
342 token_getmad($4,$$,';');
343#else
344 package($3);
345 if ($2)
346 package_version($2);
347 $$ = (OP*)NULL;
348#endif
349 }
350 | USE startsub
351 { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
09b1cffe 352 WORD WORD optlistexpr ';'
eae48c89
Z
353 {
354 SvREFCNT_inc_simple_void(PL_compcv);
355#ifdef MAD
356 $$ = utilize(IVAL($1), $2, $4, $5, $6);
357 token_getmad($1,$$,'o');
358 token_getmad($7,$$,';');
359 if (PL_parser->rsfp_filters &&
360 AvFILLp(PL_parser->rsfp_filters) >= 0)
361 append_madprops(newMADPROP('!', MAD_NULL, NULL, 0), $$, 0);
362#else
363 utilize(IVAL($1), $2, $4, $5, $6);
364 $$ = (OP*)NULL;
365#endif
366 }
367 | IF lpar_or_qw remember mexpr ')' mblock else
368 {
3ad73efd
Z
369 $$ = block_end($3,
370 newCONDOP(0, $4, op_scope($6), $7));
eae48c89
Z
371 TOKEN_GETMAD($1,$$,'I');
372 TOKEN_GETMAD($2,$$,'(');
373 TOKEN_GETMAD($5,$$,')');
374 PL_parser->copline = (line_t)IVAL($1);
375 }
376 | UNLESS lpar_or_qw remember miexpr ')' mblock else
377 {
3ad73efd
Z
378 $$ = block_end($3,
379 newCONDOP(0, $4, op_scope($6), $7));
eae48c89
Z
380 TOKEN_GETMAD($1,$$,'I');
381 TOKEN_GETMAD($2,$$,'(');
382 TOKEN_GETMAD($5,$$,')');
383 PL_parser->copline = (line_t)IVAL($1);
384 }
385 | GIVEN lpar_or_qw remember mydefsv mexpr ')' mblock
386 {
387 $$ = block_end($3,
3ad73efd 388 newGIVENOP($5, op_scope($7), (PADOFFSET)$4));
eae48c89
Z
389 PL_parser->copline = (line_t)IVAL($1);
390 }
391 | WHEN lpar_or_qw remember mexpr ')' mblock
3ad73efd 392 { $$ = block_end($3, newWHENOP($4, op_scope($6))); }
eae48c89 393 | DEFAULT block
3ad73efd 394 { $$ = newWHENOP(0, op_scope($2)); }
eae48c89
Z
395 | WHILE lpar_or_qw remember texpr ')' mintro mblock cont
396 {
397 $$ = block_end($3,
398 newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
94bf0465 399 $4, $7, $8, $6));
eae48c89
Z
400 TOKEN_GETMAD($1,$$,'W');
401 TOKEN_GETMAD($2,$$,'(');
402 TOKEN_GETMAD($5,$$,')');
403 PL_parser->copline = (line_t)IVAL($1);
404 }
405 | UNTIL lpar_or_qw remember iexpr ')' mintro mblock cont
406 {
407 $$ = block_end($3,
94bf0465
Z
408 newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
409 $4, $7, $8, $6));
eae48c89
Z
410 TOKEN_GETMAD($1,$$,'W');
411 TOKEN_GETMAD($2,$$,'(');
412 TOKEN_GETMAD($5,$$,')');
413 PL_parser->copline = (line_t)IVAL($1);
414 }
415 | FOR lpar_or_qw remember mnexpr ';' texpr ';' mintro mnexpr ')'
416 mblock
417 {
418 OP *initop = IF_MAD($4 ? $4 : newOP(OP_NULL, 0), $4);
419 OP *forop = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
94bf0465 420 scalar($6), $11, $9, $8);
eae48c89
Z
421 if (initop) {
422 forop = op_prepend_elem(OP_LINESEQ, initop,
423 op_append_elem(OP_LINESEQ,
424 newOP(OP_UNSTACK, OPf_SPECIAL),
425 forop));
5f211341 426 }
eae48c89
Z
427 DO_MAD({ forop = newUNOP(OP_NULL, 0, forop); })
428 $$ = block_end($3, forop);
429 TOKEN_GETMAD($1,$$,'3');
430 TOKEN_GETMAD($2,$$,'(');
431 TOKEN_GETMAD($5,$$,'1');
432 TOKEN_GETMAD($7,$$,'2');
433 TOKEN_GETMAD($10,$$,')');
434 PL_parser->copline = (line_t)IVAL($1);
435 }
436 | FOR MY remember my_scalar lpar_or_qw mexpr ')' mblock cont
437 {
94bf0465 438 $$ = block_end($3, newFOROP(0, $4, $6, $8, $9));
eae48c89
Z
439 TOKEN_GETMAD($1,$$,'W');
440 TOKEN_GETMAD($2,$$,'d');
441 TOKEN_GETMAD($5,$$,'(');
442 TOKEN_GETMAD($7,$$,')');
443 PL_parser->copline = (line_t)IVAL($1);
444 }
445 | FOR scalar lpar_or_qw remember mexpr ')' mblock cont
446 {
94bf0465 447 $$ = block_end($4, newFOROP(0,
3ad73efd 448 op_lvalue($2, OP_ENTERLOOP), $5, $7, $8));
eae48c89
Z
449 TOKEN_GETMAD($1,$$,'W');
450 TOKEN_GETMAD($3,$$,'(');
451 TOKEN_GETMAD($6,$$,')');
452 PL_parser->copline = (line_t)IVAL($1);
453 }
454 | FOR lpar_or_qw remember mexpr ')' mblock cont
455 {
456 $$ = block_end($3,
94bf0465 457 newFOROP(0, (OP*)NULL, $4, $6, $7));
eae48c89
Z
458 TOKEN_GETMAD($1,$$,'W');
459 TOKEN_GETMAD($2,$$,'(');
460 TOKEN_GETMAD($5,$$,')');
461 PL_parser->copline = (line_t)IVAL($1);
462 }
463 | block cont
464 {
465 /* a block is a loop that happens once */
466 $$ = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
94bf0465 467 (OP*)NULL, $1, $2, 0);
eae48c89
Z
468 }
469 | PACKAGE WORD WORD '{' remember
470 {
471 int save_3_latefree = $3->op_latefree;
472 $3->op_latefree = 1;
473 package($3);
474 $3->op_latefree = save_3_latefree;
475 if ($2) {
476 int save_2_latefree = $2->op_latefree;
477 $2->op_latefree = 1;
478 package_version($2);
479 $2->op_latefree = save_2_latefree;
480 }
481 }
482 stmtseq '}'
483 {
484 /* a block is a loop that happens once */
94bf0465 485 $$ = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
eae48c89
Z
486 (OP*)NULL, block_end($5, $7), (OP*)NULL, 0);
487 op_free($3);
488 if ($2)
489 op_free($2);
490 TOKEN_GETMAD($4,$$,'{');
491 TOKEN_GETMAD($8,$$,'}');
492 if (PL_parser->copline > (line_t)IVAL($4))
493 PL_parser->copline = (line_t)IVAL($4);
494 }
495 | sideff ';'
496 {
497 PL_parser->expect = XSTATE;
498 $$ = $1;
499 TOKEN_GETMAD($2,$$,';');
500 }
501 | ';'
502 {
503 PL_parser->expect = XSTATE;
504 $$ = IF_MAD(newOP(OP_NULL, 0), (OP*)NULL);
505 TOKEN_GETMAD($1,$$,';');
506 PL_parser->copline = NOLINE;
5f211341 507 }
8d063cd8
LW
508 ;
509
891be019 510/* An expression which may have a side-effect */
a687059c 511sideff : error
bcabcc50 512 { $$ = (OP*)NULL; }
a687059c 513 | expr
79072805 514 { $$ = $1; }
a687059c 515 | expr IF expr
f05e27e5
DM
516 { $$ = newLOGOP(OP_AND, 0, $3, $1);
517 TOKEN_GETMAD($2,$$,'i');
518 }
a687059c 519 | expr UNLESS expr
f05e27e5
DM
520 { $$ = newLOGOP(OP_OR, 0, $3, $1);
521 TOKEN_GETMAD($2,$$,'i');
522 }
a687059c 523 | expr WHILE expr
f05e27e5
DM
524 { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1);
525 TOKEN_GETMAD($2,$$,'w');
526 }
55497cff 527 | expr UNTIL iexpr
f05e27e5
DM
528 { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1);
529 TOKEN_GETMAD($2,$$,'w');
530 }
ecca16b0 531 | expr FOR expr
94bf0465 532 { $$ = newFOROP(0, (OP*)NULL, $3, $1, (OP*)NULL);
eae48c89 533 TOKEN_GETMAD($2,$$,'w');
94bf0465 534 PL_parser->copline = (line_t)IVAL($2);
f05e27e5 535 }
f20dcd76 536 | expr WHEN expr
3ad73efd 537 { $$ = newWHENOP($3, op_scope($1)); }
79072805
LW
538 ;
539
891be019 540/* else and elsif blocks */
79072805 541else : /* NULL */
bcabcc50 542 { $$ = (OP*)NULL; }
55497cff 543 | ELSE mblock
3ad73efd
Z
544 {
545 ($2)->op_flags |= OPf_PARENS;
546 $$ = op_scope($2);
f05e27e5
DM
547 TOKEN_GETMAD($1,$$,'o');
548 }
ea25a9b2 549 | ELSIF lpar_or_qw mexpr ')' mblock else
53a7735b 550 { PL_parser->copline = (line_t)IVAL($1);
3ad73efd
Z
551 $$ = newCONDOP(0,
552 newSTATEOP(OPf_SPECIAL,NULL,$3),
553 op_scope($5), $6);
554 PL_hints |= HINT_BLOCK_SCOPE;
f05e27e5
DM
555 TOKEN_GETMAD($1,$$,'I');
556 TOKEN_GETMAD($2,$$,'(');
557 TOKEN_GETMAD($4,$$,')');
558 }
79072805
LW
559 ;
560
891be019 561/* Continue blocks */
79072805 562cont : /* NULL */
bcabcc50 563 { $$ = (OP*)NULL; }
79072805 564 | CONTINUE block
3ad73efd
Z
565 {
566 $$ = op_scope($2);
f05e27e5
DM
567 TOKEN_GETMAD($1,$$,'o');
568 }
79072805
LW
569 ;
570
a034e688
DM
571/* determine whether there are any new my declarations */
572mintro : /* NULL */
573 { $$ = (PL_min_intro_pending &&
574 PL_max_intro_pending >= PL_min_intro_pending);
575 intro_my(); }
576
891be019 577/* Normal expression */
8d063cd8 578nexpr : /* NULL */
bcabcc50 579 { $$ = (OP*)NULL; }
8d063cd8
LW
580 | sideff
581 ;
582
891be019 583/* Boolean expression */
8d063cd8 584texpr : /* NULL means true */
f05e27e5
DM
585 { YYSTYPE tmplval;
586 (void)scan_num("1", &tmplval);
587 $$ = tmplval.opval; }
8d063cd8
LW
588 | expr
589 ;
590
891be019 591/* Inverted boolean expression */
55497cff
PP
592iexpr : expr
593 { $$ = invert(scalar($1)); }
594 ;
595
891be019 596/* Expression with its own lexical scope */
55497cff 597mexpr : expr
bbce6d69
PP
598 { $$ = $1; intro_my(); }
599 ;
600
601mnexpr : nexpr
602 { $$ = $1; intro_my(); }
55497cff
PP
603 ;
604
55497cff 605miexpr : iexpr
bbce6d69 606 { $$ = $1; intro_my(); }
55497cff
PP
607 ;
608
44a8e56a 609formname: WORD { $$ = $1; }
bcabcc50 610 | /* NULL */ { $$ = (OP*)NULL; }
8d063cd8
LW
611 ;
612
fa83b5b6 613startsub: /* NULL */ /* start a regular subroutine scope */
a8ff2fa6
DM
614 { $$ = start_subparse(FALSE, 0);
615 SAVEFREESV(PL_compcv); }
f05e27e5 616
28757baa
PP
617 ;
618
619startanonsub: /* NULL */ /* start an anonymous subroutine scope */
a8ff2fa6
DM
620 { $$ = start_subparse(FALSE, CVf_ANON);
621 SAVEFREESV(PL_compcv); }
28757baa
PP
622 ;
623
44a8e56a 624startformsub: /* NULL */ /* start a format subroutine scope */
a8ff2fa6
DM
625 { $$ = start_subparse(TRUE, 0);
626 SAVEFREESV(PL_compcv); }
44a8e56a
PP
627 ;
628
891be019 629/* Name of a subroutine - must be a bareword, could be special */
2596d9fe 630subname : WORD { const char *const name = SvPV_nolen_const(((SVOP*)$1)->op_sv);
e858de61 631 if (strEQ(name, "BEGIN") || strEQ(name, "END")
3c10abe3
AG
632 || strEQ(name, "INIT") || strEQ(name, "CHECK")
633 || strEQ(name, "UNITCHECK"))
1aff0e91 634 CvSPECIAL_on(PL_compcv);
28757baa 635 $$ = $1; }
a0d0e21e
LW
636 ;
637
891be019 638/* Subroutine prototype */
4633a7c4 639proto : /* NULL */
bcabcc50 640 { $$ = (OP*)NULL; }
4633a7c4
LW
641 | THING
642 ;
28757baa 643
891be019 644/* Optional list of subroutine attributes */
09bef843 645subattrlist: /* NULL */
bcabcc50 646 { $$ = (OP*)NULL; }
09bef843 647 | COLONATTR THING
f05e27e5
DM
648 { $$ = $2;
649 TOKEN_GETMAD($1,$$,':');
650 }
09bef843 651 | COLONATTR
f05e27e5
DM
652 { $$ = IF_MAD(
653 newOP(OP_NULL, 0),
bcabcc50 654 (OP*)NULL
f05e27e5
DM
655 );
656 TOKEN_GETMAD($1,$$,':');
657 }
09bef843
SB
658 ;
659
891be019 660/* List of attributes for a "my" variable declaration */
09bef843 661myattrlist: COLONATTR THING
f05e27e5
DM
662 { $$ = $2;
663 TOKEN_GETMAD($1,$$,':');
664 }
09bef843 665 | COLONATTR
f05e27e5
DM
666 { $$ = IF_MAD(
667 newOP(OP_NULL, 0),
bcabcc50 668 (OP*)NULL
f05e27e5
DM
669 );
670 TOKEN_GETMAD($1,$$,':');
671 }
09bef843
SB
672 ;
673
891be019 674/* Subroutine body - either null or a block */
28757baa 675subbody : block { $$ = $1; }
f05e27e5
DM
676 | ';' { $$ = IF_MAD(
677 newOP(OP_NULL,0),
bcabcc50 678 (OP*)NULL
f05e27e5 679 );
53a7735b 680 PL_parser->expect = XSTATE;
f05e27e5
DM
681 TOKEN_GETMAD($1,$$,';');
682 }
8d063cd8
LW
683 ;
684
891be019 685/* Ordinary expressions; logical combinations */
a0d0e21e 686expr : expr ANDOP expr
f05e27e5
DM
687 { $$ = newLOGOP(OP_AND, 0, $1, $3);
688 TOKEN_GETMAD($2,$$,'o');
689 }
a0d0e21e 690 | expr OROP expr
f05e27e5
DM
691 { $$ = newLOGOP(IVAL($2), 0, $1, $3);
692 TOKEN_GETMAD($2,$$,'o');
693 }
c963b151 694 | expr DOROP expr
f05e27e5
DM
695 { $$ = newLOGOP(OP_DOR, 0, $1, $3);
696 TOKEN_GETMAD($2,$$,'o');
697 }
09b1cffe 698 | listexpr %prec PREC_LOW
a0d0e21e
LW
699 ;
700
891be019 701/* Expressions are a list of terms joined by commas */
09b1cffe 702listexpr: listexpr ','
f05e27e5
DM
703 {
704#ifdef MAD
705 OP* op = newNULLLIST();
706 token_getmad($2,op,',');
2fcb4757 707 $$ = op_append_elem(OP_LIST, $1, op);
f05e27e5
DM
708#else
709 $$ = $1;
710#endif
711 }
09b1cffe 712 | listexpr ',' term
f05e27e5 713 {
29522234 714 OP* term = $3;
f05e27e5 715 DO_MAD(
29522234
DM
716 term = newUNOP(OP_NULL, 0, term);
717 token_getmad($2,term,',');
f05e27e5 718 )
2fcb4757 719 $$ = op_append_elem(OP_LIST, $1, term);
f05e27e5 720 }
fad39ff1 721 | term %prec PREC_LOW
8d063cd8
LW
722 ;
723
891be019 724/* List operators */
09b1cffe 725listop : LSTOP indirob listexpr /* map {...} @args or print $fh @args */
f05e27e5 726 { $$ = convert(IVAL($1), OPf_STACKED,
2fcb4757 727 op_prepend_elem(OP_LIST, newGVREF(IVAL($1),$2), $3) );
f05e27e5
DM
728 TOKEN_GETMAD($1,$$,'o');
729 }
891be019 730 | FUNC '(' indirob expr ')' /* print ($fh @args */
f05e27e5 731 { $$ = convert(IVAL($1), OPf_STACKED,
2fcb4757 732 op_prepend_elem(OP_LIST, newGVREF(IVAL($1),$3), $4) );
f05e27e5
DM
733 TOKEN_GETMAD($1,$$,'o');
734 TOKEN_GETMAD($2,$$,'(');
735 TOKEN_GETMAD($5,$$,')');
736 }
09b1cffe 737 | term ARROW method lpar_or_qw optexpr ')' /* $foo->bar(list) */
4633a7c4 738 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
2fcb4757
Z
739 op_append_elem(OP_LIST,
740 op_prepend_elem(OP_LIST, scalar($1), $5),
f05e27e5
DM
741 newUNOP(OP_METHOD, 0, $3)));
742 TOKEN_GETMAD($2,$$,'A');
743 TOKEN_GETMAD($4,$$,'(');
744 TOKEN_GETMAD($6,$$,')');
745 }
891be019 746 | term ARROW method /* $foo->bar */
b1524f17 747 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
2fcb4757 748 op_append_elem(OP_LIST, scalar($1),
f05e27e5
DM
749 newUNOP(OP_METHOD, 0, $3)));
750 TOKEN_GETMAD($2,$$,'A');
751 }
09b1cffe 752 | METHOD indirob optlistexpr /* new Class @args */
4633a7c4 753 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
2fcb4757
Z
754 op_append_elem(OP_LIST,
755 op_prepend_elem(OP_LIST, $2, $3),
f05e27e5
DM
756 newUNOP(OP_METHOD, 0, $1)));
757 }
09b1cffe 758 | FUNCMETH indirob '(' optexpr ')' /* method $object (@args) */
4633a7c4 759 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
2fcb4757
Z
760 op_append_elem(OP_LIST,
761 op_prepend_elem(OP_LIST, $2, $4),
f05e27e5
DM
762 newUNOP(OP_METHOD, 0, $1)));
763 TOKEN_GETMAD($3,$$,'(');
764 TOKEN_GETMAD($5,$$,')');
765 }
09b1cffe 766 | LSTOP optlistexpr /* print @args */
f05e27e5
DM
767 { $$ = convert(IVAL($1), 0, $2);
768 TOKEN_GETMAD($1,$$,'o');
769 }
09b1cffe 770 | FUNC '(' optexpr ')' /* print (@args) */
f05e27e5
DM
771 { $$ = convert(IVAL($1), 0, $3);
772 TOKEN_GETMAD($1,$$,'o');
773 TOKEN_GETMAD($2,$$,'(');
774 TOKEN_GETMAD($4,$$,')');
775 }
718a7425 776 | LSTOPSUB startanonsub block /* sub f(&@); f { foo } ... */
5a5094bd 777 { SvREFCNT_inc_simple_void(PL_compcv);
bcabcc50 778 $<opval>$ = newANONATTRSUB($2, 0, (OP*)NULL, $3); }
09b1cffe 779 optlistexpr %prec LSTOP /* ... @bar */
4633a7c4 780 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
2fcb4757
Z
781 op_append_elem(OP_LIST,
782 op_prepend_elem(OP_LIST, $<opval>4, $5), $1));
f05e27e5 783 }
a687059c
LW
784 ;
785
891be019 786/* Names of methods. May use $object->$methodname */
a0d0e21e
LW
787method : METHOD
788 | scalar
789 ;
790
891be019
SC
791/* Some kind of subscripted expression */
792subscripted: star '{' expr ';' '}' /* *main::{something} */
793 /* In this and all the hash accessors, ';' is
794 * provided by the tokeniser */
264e1af3 795 { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3));
53a7735b 796 PL_parser->expect = XOPERATOR;
f05e27e5
DM
797 TOKEN_GETMAD($2,$$,'{');
798 TOKEN_GETMAD($4,$$,';');
799 TOKEN_GETMAD($5,$$,'}');
800 }
891be019 801 | scalar '[' expr ']' /* $array[$element] */
f05e27e5
DM
802 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3));
803 TOKEN_GETMAD($2,$$,'[');
804 TOKEN_GETMAD($4,$$,']');
805 }
891be019 806 | term ARROW '[' expr ']' /* somearef->[$element] */
fad39ff1
SM
807 { $$ = newBINOP(OP_AELEM, 0,
808 ref(newAVREF($1),OP_RV2AV),
f05e27e5
DM
809 scalar($4));
810 TOKEN_GETMAD($2,$$,'a');
811 TOKEN_GETMAD($3,$$,'[');
812 TOKEN_GETMAD($5,$$,']');
813 }
891be019 814 | subscripted '[' expr ']' /* $foo->[$bar]->[$baz] */
fad39ff1
SM
815 { $$ = newBINOP(OP_AELEM, 0,
816 ref(newAVREF($1),OP_RV2AV),
f05e27e5
DM
817 scalar($3));
818 TOKEN_GETMAD($2,$$,'[');
819 TOKEN_GETMAD($4,$$,']');
820 }
5a63ceef 821 | scalar '{' expr ';' '}' /* $foo{bar();} */
fad39ff1 822 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
53a7735b 823 PL_parser->expect = XOPERATOR;
f05e27e5
DM
824 TOKEN_GETMAD($2,$$,'{');
825 TOKEN_GETMAD($4,$$,';');
826 TOKEN_GETMAD($5,$$,'}');
827 }
891be019 828 | term ARROW '{' expr ';' '}' /* somehref->{bar();} */
fad39ff1
SM
829 { $$ = newBINOP(OP_HELEM, 0,
830 ref(newHVREF($1),OP_RV2HV),
831 jmaybe($4));
53a7735b 832 PL_parser->expect = XOPERATOR;
f05e27e5
DM
833 TOKEN_GETMAD($2,$$,'a');
834 TOKEN_GETMAD($3,$$,'{');
835 TOKEN_GETMAD($5,$$,';');
836 TOKEN_GETMAD($6,$$,'}');
837 }
891be019 838 | subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */
fad39ff1
SM
839 { $$ = newBINOP(OP_HELEM, 0,
840 ref(newHVREF($1),OP_RV2HV),
841 jmaybe($3));
53a7735b 842 PL_parser->expect = XOPERATOR;
f05e27e5
DM
843 TOKEN_GETMAD($2,$$,'{');
844 TOKEN_GETMAD($4,$$,';');
845 TOKEN_GETMAD($5,$$,'}');
846 }
891be019 847 | term ARROW '(' ')' /* $subref->() */
fad39ff1 848 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
f05e27e5
DM
849 newCVREF(0, scalar($1)));
850 TOKEN_GETMAD($2,$$,'a');
851 TOKEN_GETMAD($3,$$,'(');
852 TOKEN_GETMAD($4,$$,')');
853 }
891be019 854 | term ARROW '(' expr ')' /* $subref->(@args) */
fad39ff1 855 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
2fcb4757 856 op_append_elem(OP_LIST, $4,
f05e27e5
DM
857 newCVREF(0, scalar($1))));
858 TOKEN_GETMAD($2,$$,'a');
859 TOKEN_GETMAD($3,$$,'(');
860 TOKEN_GETMAD($5,$$,')');
861 }
fad39ff1 862
ea25a9b2 863 | subscripted lpar_or_qw expr ')' /* $foo->{bar}->(@args) */
fad39ff1 864 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
2fcb4757 865 op_append_elem(OP_LIST, $3,
f05e27e5
DM
866 newCVREF(0, scalar($1))));
867 TOKEN_GETMAD($2,$$,'(');
868 TOKEN_GETMAD($4,$$,')');
869 }
ea25a9b2 870 | subscripted lpar_or_qw ')' /* $foo->{bar}->() */
fad39ff1 871 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
f05e27e5
DM
872 newCVREF(0, scalar($1)));
873 TOKEN_GETMAD($2,$$,'(');
874 TOKEN_GETMAD($3,$$,')');
875 }
9a9798c2 876 | '(' expr ')' '[' expr ']' /* list slice */
f05e27e5
DM
877 { $$ = newSLICEOP(0, $5, $2);
878 TOKEN_GETMAD($1,$$,'(');
879 TOKEN_GETMAD($3,$$,')');
880 TOKEN_GETMAD($4,$$,'[');
881 TOKEN_GETMAD($6,$$,']');
882 }
ea25a9b2
Z
883 | QWLIST '[' expr ']' /* list literal slice */
884 { $$ = newSLICEOP(0, $3, $1);
885 TOKEN_GETMAD($2,$$,'[');
886 TOKEN_GETMAD($4,$$,']');
887 }
9a9798c2 888 | '(' ')' '[' expr ']' /* empty list slice! */
bcabcc50 889 { $$ = newSLICEOP(0, $4, (OP*)NULL);
f05e27e5
DM
890 TOKEN_GETMAD($1,$$,'(');
891 TOKEN_GETMAD($2,$$,')');
892 TOKEN_GETMAD($3,$$,'[');
893 TOKEN_GETMAD($5,$$,']');
894 }
891be019 895 ;
fad39ff1 896
891be019 897/* Binary operators between terms */
f05e27e5
DM
898termbinop: term ASSIGNOP term /* $x = $y */
899 { $$ = newASSIGNOP(OPf_STACKED, $1, IVAL($2), $3);
900 TOKEN_GETMAD($2,$$,'o');
901 }
891be019 902 | term POWOP term /* $x ** $y */
f05e27e5
DM
903 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
904 TOKEN_GETMAD($2,$$,'o');
905 }
891be019 906 | term MULOP term /* $x * $y, $x x $y */
f05e27e5 907 { if (IVAL($2) != OP_REPEAT)
79072805 908 scalar($1);
f05e27e5
DM
909 $$ = newBINOP(IVAL($2), 0, $1, scalar($3));
910 TOKEN_GETMAD($2,$$,'o');
911 }
891be019 912 | term ADDOP term /* $x + $y */
f05e27e5
DM
913 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
914 TOKEN_GETMAD($2,$$,'o');
915 }
891be019 916 | term SHIFTOP term /* $x >> $y, $x << $y */
f05e27e5
DM
917 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
918 TOKEN_GETMAD($2,$$,'o');
919 }
891be019 920 | term RELOP term /* $x > $y, etc. */
f05e27e5
DM
921 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
922 TOKEN_GETMAD($2,$$,'o');
923 }
891be019 924 | term EQOP term /* $x == $y, $x eq $y */
f05e27e5
DM
925 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
926 TOKEN_GETMAD($2,$$,'o');
927 }
891be019 928 | term BITANDOP term /* $x & $y */
f05e27e5
DM
929 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
930 TOKEN_GETMAD($2,$$,'o');
931 }
891be019 932 | term BITOROP term /* $x | $y */
f05e27e5
DM
933 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
934 TOKEN_GETMAD($2,$$,'o');
935 }
891be019 936 | term DOTDOT term /* $x..$y, $x...$y */
f05e27e5
DM
937 {
938 $$ = newRANGE(IVAL($2), scalar($1), scalar($3));
035e2bcc 939 DO_MAD({
f05e27e5
DM
940 UNOP *op;
941 op = (UNOP*)$$;
942 op = (UNOP*)op->op_first; /* get to flop */
943 op = (UNOP*)op->op_first; /* get to flip */
944 op = (UNOP*)op->op_first; /* get to range */
945 token_getmad($2,(OP*)op,'o');
035e2bcc 946 })
f05e27e5 947 }
891be019 948 | term ANDAND term /* $x && $y */
f05e27e5
DM
949 { $$ = newLOGOP(OP_AND, 0, $1, $3);
950 TOKEN_GETMAD($2,$$,'o');
951 }
891be019 952 | term OROR term /* $x || $y */
f05e27e5
DM
953 { $$ = newLOGOP(OP_OR, 0, $1, $3);
954 TOKEN_GETMAD($2,$$,'o');
955 }
c963b151 956 | term DORDOR term /* $x // $y */
f05e27e5
DM
957 { $$ = newLOGOP(OP_DOR, 0, $1, $3);
958 TOKEN_GETMAD($2,$$,'o');
959 }
891be019 960 | term MATCHOP term /* $x =~ /$y/ */
f05e27e5
DM
961 { $$ = bind_match(IVAL($2), $1, $3);
962 TOKEN_GETMAD($2,
963 ($$->op_type == OP_NOT
964 ? ((UNOP*)$$)->op_first : $$),
965 '~');
966 }
891be019 967 ;
8d063cd8 968
891be019
SC
969/* Unary operators and terms */
970termunop : '-' term %prec UMINUS /* -$x */
f05e27e5
DM
971 { $$ = newUNOP(OP_NEGATE, 0, scalar($2));
972 TOKEN_GETMAD($1,$$,'o');
973 }
891be019 974 | '+' term %prec UMINUS /* +$x */
f05e27e5
DM
975 { $$ = IF_MAD(
976 newUNOP(OP_NULL, 0, $2),
977 $2
978 );
979 TOKEN_GETMAD($1,$$,'+');
980 }
891be019 981 | '!' term /* !$x */
f05e27e5
DM
982 { $$ = newUNOP(OP_NOT, 0, scalar($2));
983 TOKEN_GETMAD($1,$$,'o');
984 }
891be019 985 | '~' term /* ~$x */
f05e27e5
DM
986 { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));
987 TOKEN_GETMAD($1,$$,'o');
988 }
891be019 989 | term POSTINC /* $x++ */
79072805 990 { $$ = newUNOP(OP_POSTINC, 0,
3ad73efd 991 op_lvalue(scalar($1), OP_POSTINC));
f05e27e5
DM
992 TOKEN_GETMAD($2,$$,'o');
993 }
891be019 994 | term POSTDEC /* $x-- */
79072805 995 { $$ = newUNOP(OP_POSTDEC, 0,
3ad73efd 996 op_lvalue(scalar($1), OP_POSTDEC));
f05e27e5
DM
997 TOKEN_GETMAD($2,$$,'o');
998 }
891be019 999 | PREINC term /* ++$x */
79072805 1000 { $$ = newUNOP(OP_PREINC, 0,
3ad73efd 1001 op_lvalue(scalar($2), OP_PREINC));
f05e27e5
DM
1002 TOKEN_GETMAD($1,$$,'o');
1003 }
891be019 1004 | PREDEC term /* --$x */
79072805 1005 { $$ = newUNOP(OP_PREDEC, 0,
3ad73efd 1006 op_lvalue(scalar($2), OP_PREDEC));
f05e27e5
DM
1007 TOKEN_GETMAD($1,$$,'o');
1008 }
891be019
SC
1009
1010 ;
1011
1012/* Constructors for anonymous data */
1013anonymous: '[' expr ']'
f05e27e5
DM
1014 { $$ = newANONLIST($2);
1015 TOKEN_GETMAD($1,$$,'[');
1016 TOKEN_GETMAD($3,$$,']');
1017 }
891be019 1018 | '[' ']'
bcabcc50 1019 { $$ = newANONLIST((OP*)NULL);
f05e27e5
DM
1020 TOKEN_GETMAD($1,$$,'[');
1021 TOKEN_GETMAD($2,$$,']');
1022 }
891be019 1023 | HASHBRACK expr ';' '}' %prec '(' /* { foo => "Bar" } */
f05e27e5
DM
1024 { $$ = newANONHASH($2);
1025 TOKEN_GETMAD($1,$$,'{');
1026 TOKEN_GETMAD($3,$$,';');
1027 TOKEN_GETMAD($4,$$,'}');
1028 }
891be019 1029 | HASHBRACK ';' '}' %prec '(' /* { } (';' by tokener) */
bcabcc50 1030 { $$ = newANONHASH((OP*)NULL);
f05e27e5
DM
1031 TOKEN_GETMAD($1,$$,'{');
1032 TOKEN_GETMAD($2,$$,';');
1033 TOKEN_GETMAD($3,$$,'}');
1034 }
718a7425 1035 | ANONSUB startanonsub proto subattrlist block %prec '('
5a5094bd 1036 { SvREFCNT_inc_simple_void(PL_compcv);
718a7425 1037 $$ = newANONATTRSUB($2, $3, $4, $5);
f05e27e5 1038 TOKEN_GETMAD($1,$$,'o');
718a7425
DM
1039 OP_GETMAD($3,$$,'s');
1040 OP_GETMAD($4,$$,'a');
f05e27e5 1041 }
891be019
SC
1042
1043 ;
1044
1045/* Things called with "do" */
1046termdo : DO term %prec UNIOP /* do $filename */
123d08c9 1047 { $$ = dofile($2, IVAL($1));
f05e27e5
DM
1048 TOKEN_GETMAD($1,$$,'o');
1049 }
891be019 1050 | DO block %prec '(' /* do { code */
3ad73efd 1051 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, op_scope($2));
f05e27e5
DM
1052 TOKEN_GETMAD($1,$$,'D');
1053 }
ea25a9b2 1054 | DO WORD lpar_or_qw ')' /* do somesub() */
891be019
SC
1055 { $$ = newUNOP(OP_ENTERSUB,
1056 OPf_SPECIAL|OPf_STACKED,
2fcb4757 1057 op_prepend_elem(OP_LIST,
891be019
SC
1058 scalar(newCVREF(
1059 (OPpENTERSUB_AMPER<<8),
1060 scalar($2)
bcabcc50 1061 )),(OP*)NULL)); dep();
f05e27e5
DM
1062 TOKEN_GETMAD($1,$$,'o');
1063 TOKEN_GETMAD($3,$$,'(');
1064 TOKEN_GETMAD($4,$$,')');
1065 }
ea25a9b2 1066 | DO WORD lpar_or_qw expr ')' /* do somesub(@args) */
891be019
SC
1067 { $$ = newUNOP(OP_ENTERSUB,
1068 OPf_SPECIAL|OPf_STACKED,
2fcb4757 1069 op_append_elem(OP_LIST,
891be019
SC
1070 $4,
1071 scalar(newCVREF(
1072 (OPpENTERSUB_AMPER<<8),
1073 scalar($2)
f05e27e5
DM
1074 )))); dep();
1075 TOKEN_GETMAD($1,$$,'o');
1076 TOKEN_GETMAD($3,$$,'(');
1077 TOKEN_GETMAD($5,$$,')');
1078 }
ea25a9b2 1079 | DO scalar lpar_or_qw ')' /* do $subref () */
891be019 1080 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
2fcb4757 1081 op_prepend_elem(OP_LIST,
bcabcc50 1082 scalar(newCVREF(0,scalar($2))), (OP*)NULL)); dep();
f05e27e5
DM
1083 TOKEN_GETMAD($1,$$,'o');
1084 TOKEN_GETMAD($3,$$,'(');
1085 TOKEN_GETMAD($4,$$,')');
1086 }
ea25a9b2 1087 | DO scalar lpar_or_qw expr ')' /* do $subref (@args) */
891be019 1088 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
2fcb4757 1089 op_prepend_elem(OP_LIST,
891be019 1090 $4,
f05e27e5
DM
1091 scalar(newCVREF(0,scalar($2))))); dep();
1092 TOKEN_GETMAD($1,$$,'o');
1093 TOKEN_GETMAD($3,$$,'(');
1094 TOKEN_GETMAD($5,$$,')');
1095 }
891be019
SC
1096
1097 ;
1098
1099term : termbinop
1100 | termunop
1101 | anonymous
1102 | termdo
e9bdcc27 1103 | term '?' term ':' term
f05e27e5
DM
1104 { $$ = newCONDOP(0, $1, $3, $5);
1105 TOKEN_GETMAD($2,$$,'?');
1106 TOKEN_GETMAD($4,$$,':');
1107 }
891be019 1108 | REFGEN term /* \$x, \@y, \%z */
3ad73efd 1109 { $$ = newUNOP(OP_REFGEN, 0, op_lvalue($2,OP_REFGEN));
f05e27e5
DM
1110 TOKEN_GETMAD($1,$$,'o');
1111 }
09bef843
SB
1112 | myattrterm %prec UNIOP
1113 { $$ = $1; }
1114 | LOCAL term %prec UNIOP
f05e27e5 1115 { $$ = localize($2,IVAL($1));
dde83331 1116 TOKEN_GETMAD($1,$$,'k');
f05e27e5 1117 }
a0d0e21e 1118 | '(' expr ')'
f05e27e5
DM
1119 { $$ = sawparens(IF_MAD(newUNOP(OP_NULL,0,$2), $2));
1120 TOKEN_GETMAD($1,$$,'(');
1121 TOKEN_GETMAD($3,$$,')');
1122 }
ea25a9b2
Z
1123 | QWLIST
1124 { $$ = IF_MAD(newUNOP(OP_NULL,0,$1), $1); }
8d063cd8 1125 | '(' ')'
f05e27e5
DM
1126 { $$ = sawparens(newNULLLIST());
1127 TOKEN_GETMAD($1,$$,'(');
1128 TOKEN_GETMAD($2,$$,')');
1129 }
79072805 1130 | scalar %prec '('
8d063cd8 1131 { $$ = $1; }
79072805 1132 | star %prec '('
8d063cd8 1133 { $$ = $1; }
79072805 1134 | hsh %prec '('
8d063cd8 1135 { $$ = $1; }
79072805 1136 | ary %prec '('
8d063cd8 1137 { $$ = $1; }
891be019 1138 | arylen %prec '(' /* $#x, $#{ something } */
79072805 1139 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
fad39ff1
SM
1140 | subscripted
1141 { $$ = $1; }
891be019 1142 | ary '[' expr ']' /* array slice */
2fcb4757 1143 { $$ = op_prepend_elem(OP_ASLICE,
79072805 1144 newOP(OP_PUSHMARK, 0),
79072805
LW
1145 newLISTOP(OP_ASLICE, 0,
1146 list($3),
f05e27e5
DM
1147 ref($1, OP_ASLICE)));
1148 TOKEN_GETMAD($2,$$,'[');
1149 TOKEN_GETMAD($4,$$,']');
1150 }
891be019 1151 | ary '{' expr ';' '}' /* @hash{@keys} */
2fcb4757 1152 { $$ = op_prepend_elem(OP_HSLICE,
79072805 1153 newOP(OP_PUSHMARK, 0),
79072805
LW
1154 newLISTOP(OP_HSLICE, 0,
1155 list($3),
a0d0e21e 1156 ref(oopsHV($1), OP_HSLICE)));
53a7735b 1157 PL_parser->expect = XOPERATOR;
f05e27e5
DM
1158 TOKEN_GETMAD($2,$$,'{');
1159 TOKEN_GETMAD($4,$$,';');
1160 TOKEN_GETMAD($5,$$,'}');
1161 }
79072805
LW
1162 | THING %prec '('
1163 { $$ = $1; }
891be019 1164 | amper /* &foo; */
c07a80fd 1165 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
ea25a9b2 1166 | amper lpar_or_qw ')' /* &foo() */
f05e27e5
DM
1167 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1));
1168 TOKEN_GETMAD($2,$$,'(');
1169 TOKEN_GETMAD($3,$$,')');
1170 }
ea25a9b2 1171 | amper lpar_or_qw expr ')' /* &foo(@args) */
f05e27e5
DM
1172 {
1173 $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
2fcb4757 1174 op_append_elem(OP_LIST, $3, scalar($1)));
035e2bcc 1175 DO_MAD({
f05e27e5
DM
1176 OP* op = $$;
1177 if (op->op_type == OP_CONST) { /* defeat const fold */
1178 op = (OP*)op->op_madprop->mad_val;
1179 }
1180 token_getmad($2,op,'(');
1181 token_getmad($4,op,')');
035e2bcc 1182 })
f05e27e5 1183 }
09b1cffe 1184 | NOAMP WORD optlistexpr /* foo(@args) */
a0d0e21e 1185 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
2fcb4757 1186 op_append_elem(OP_LIST, $3, scalar($2)));
f05e27e5
DM
1187 TOKEN_GETMAD($1,$$,'o');
1188 }
891be019 1189 | LOOPEX /* loop exiting command (goto, last, dump, etc) */
f05e27e5
DM
1190 { $$ = newOP(IVAL($1), OPf_SPECIAL);
1191 PL_hints |= HINT_BLOCK_SCOPE;
1192 TOKEN_GETMAD($1,$$,'o');
1193 }
a0d0e21e 1194 | LOOPEX term
f05e27e5
DM
1195 { $$ = newLOOPEX(IVAL($1),$2);
1196 TOKEN_GETMAD($1,$$,'o');
1197 }
09b1cffe 1198 | NOTOP listexpr /* not $foo */
f05e27e5
DM
1199 { $$ = newUNOP(OP_NOT, 0, scalar($2));
1200 TOKEN_GETMAD($1,$$,'o');
1201 }
891be019 1202 | UNIOP /* Unary op, $_ implied */
f05e27e5
DM
1203 { $$ = newOP(IVAL($1), 0);
1204 TOKEN_GETMAD($1,$$,'o');
1205 }
1206 | UNIOP block /* eval { foo }* */
1207 { $$ = newUNOP(IVAL($1), 0, $2);
1208 TOKEN_GETMAD($1,$$,'o');
1209 }
891be019 1210 | UNIOP term /* Unary op */
f05e27e5
DM
1211 { $$ = newUNOP(IVAL($1), 0, $2);
1212 TOKEN_GETMAD($1,$$,'o');
1213 }
d2fdf8fd
RGS
1214 | REQUIRE /* require, $_ implied */
1215 { $$ = newOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0);
1216 TOKEN_GETMAD($1,$$,'o');
1217 }
1218 | REQUIRE term /* require Foo */
1219 { $$ = newUNOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0, $2);
1220 TOKEN_GETMAD($1,$$,'o');
1221 }
3cd0a11a
RGS
1222 | UNIOPSUB
1223 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
891be019 1224 | UNIOPSUB term /* Sub treated as unop */
4633a7c4 1225 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
2fcb4757 1226 op_append_elem(OP_LIST, $2, scalar($1))); }
891be019 1227 | FUNC0 /* Nullary operator */
f05e27e5
DM
1228 { $$ = newOP(IVAL($1), 0);
1229 TOKEN_GETMAD($1,$$,'o');
1230 }
ae986130 1231 | FUNC0 '(' ')'
f05e27e5
DM
1232 { $$ = newOP(IVAL($1), 0);
1233 TOKEN_GETMAD($1,$$,'o');
1234 TOKEN_GETMAD($2,$$,'(');
1235 TOKEN_GETMAD($3,$$,')');
1236 }
7eb971ee
FC
1237 | FUNC0OP /* Same as above, but op created in toke.c */
1238 { $$ = $1; }
1239 | FUNC0OP '(' ')'
1240 { $$ = $1;
1241 TOKEN_GETMAD($2,$$,'(');
1242 TOKEN_GETMAD($3,$$,')');
1243 }
891be019 1244 | FUNC0SUB /* Sub treated as nullop */
28757baa 1245 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
4633a7c4 1246 scalar($1)); }
891be019 1247 | FUNC1 '(' ')' /* not () */
f05e27e5
DM
1248 { $$ = (IVAL($1) == OP_NOT)
1249 ? newUNOP(IVAL($1), 0, newSVOP(OP_CONST, 0, newSViv(0)))
1250 : newOP(IVAL($1), OPf_SPECIAL);
1251
1252 TOKEN_GETMAD($1,$$,'o');
1253 TOKEN_GETMAD($2,$$,'(');
1254 TOKEN_GETMAD($3,$$,')');
1255 }
891be019 1256 | FUNC1 '(' expr ')' /* not($foo) */
f05e27e5
DM
1257 { $$ = newUNOP(IVAL($1), 0, $3);
1258 TOKEN_GETMAD($1,$$,'o');
1259 TOKEN_GETMAD($2,$$,'(');
1260 TOKEN_GETMAD($4,$$,')');
1261 }
09b1cffe 1262 | PMFUNC '(' listexpr ')' /* m//, s///, tr/// */
f05e27e5
DM
1263 { $$ = pmruntime($1, $3, 1);
1264 TOKEN_GETMAD($2,$$,'(');
1265 TOKEN_GETMAD($4,$$,')');
1266 }
79072805 1267 | WORD
378cc40b 1268 | listop
be25f609 1269 | YADAYADA
1270 {
1271 $$ = newLISTOP(OP_DIE, 0, newOP(OP_PUSHMARK, 0),
1272 newSVOP(OP_CONST, 0, newSVpvs("Unimplemented")));
950f2ea2 1273 TOKEN_GETMAD($1,$$,'X');
be25f609 1274 }
88e1f1a2 1275 | PLUGEXPR
8d063cd8
LW
1276 ;
1277
891be019 1278/* "my" declarations, with optional attributes */
09bef843 1279myattrterm: MY myterm myattrlist
f05e27e5
DM
1280 { $$ = my_attrs($2,$3);
1281 DO_MAD(
1282 token_getmad($1,$$,'d');
1283 append_madprops($3->op_madprop, $$, 'a');
1284 $3->op_madprop = 0;
1285 )
1286 }
09bef843 1287 | MY myterm
f05e27e5
DM
1288 { $$ = localize($2,IVAL($1));
1289 TOKEN_GETMAD($1,$$,'d');
1290 }
09bef843
SB
1291 ;
1292
891be019 1293/* Things that can be "my"'d */
09bef843 1294myterm : '(' expr ')'
f05e27e5
DM
1295 { $$ = sawparens($2);
1296 TOKEN_GETMAD($1,$$,'(');
1297 TOKEN_GETMAD($3,$$,')');
1298 }
09bef843 1299 | '(' ')'
f05e27e5
DM
1300 { $$ = sawparens(newNULLLIST());
1301 TOKEN_GETMAD($1,$$,'(');
1302 TOKEN_GETMAD($2,$$,')');
1303 }
09bef843
SB
1304 | scalar %prec '('
1305 { $$ = $1; }
1306 | hsh %prec '('
1307 { $$ = $1; }
1308 | ary %prec '('
1309 { $$ = $1; }
1310 ;
1311
891be019 1312/* Basic list expressions */
09b1cffe 1313optlistexpr: /* NULL */ %prec PREC_LOW
bcabcc50 1314 { $$ = (OP*)NULL; }
09b1cffe 1315 | listexpr %prec PREC_LOW
a0d0e21e
LW
1316 { $$ = $1; }
1317 ;
1318
09b1cffe 1319optexpr: /* NULL */
bcabcc50 1320 { $$ = (OP*)NULL; }
79072805
LW
1321 | expr
1322 { $$ = $1; }
1323 ;
1324
ea25a9b2
Z
1325lpar_or_qw: '('
1326 { $$ = $1; }
1327 | QWLIST
f7e3d326 1328 { munge_qwlist_to_paren_list($1); }
ea25a9b2
Z
1329 '('
1330 { $$ = $3; }
1331 ;
1332
891be019
SC
1333/* A little bit of trickery to make "for my $foo (@bar)" actually be
1334 lexical */
55497cff 1335my_scalar: scalar
12bd6ede 1336 { PL_parser->in_my = 0; $$ = my($1); }
55497cff
PP
1337 ;
1338
79072805 1339amper : '&' indirob
f05e27e5
DM
1340 { $$ = newCVREF(IVAL($1),$2);
1341 TOKEN_GETMAD($1,$$,'&');
1342 }
a687059c
LW
1343 ;
1344
79072805 1345scalar : '$' indirob
f05e27e5
DM
1346 { $$ = newSVREF($2);
1347 TOKEN_GETMAD($1,$$,'$');
1348 }
a687059c
LW
1349 ;
1350
79072805 1351ary : '@' indirob
f05e27e5
DM
1352 { $$ = newAVREF($2);
1353 TOKEN_GETMAD($1,$$,'@');
1354 }
79072805
LW
1355 ;
1356
1357hsh : '%' indirob
f05e27e5
DM
1358 { $$ = newHVREF($2);
1359 TOKEN_GETMAD($1,$$,'%');
1360 }
79072805
LW
1361 ;
1362
1363arylen : DOLSHARP indirob
f05e27e5
DM
1364 { $$ = newAVREF($2);
1365 TOKEN_GETMAD($1,$$,'l');
1366 }
79072805
LW
1367 ;
1368
1369star : '*' indirob
f05e27e5
DM
1370 { $$ = newGVREF(0,$2);
1371 TOKEN_GETMAD($1,$$,'*');
1372 }
79072805
LW
1373 ;
1374
891be019 1375/* Indirect objects */
79072805
LW
1376indirob : WORD
1377 { $$ = scalar($1); }
fad39ff1 1378 | scalar %prec PREC_LOW
f05e27e5 1379 { $$ = scalar($1); }
79072805 1380 | block
3ad73efd 1381 { $$ = op_scope($1); }
79072805 1382
93a17b20
LW
1383 | PRIVATEREF
1384 { $$ = $1; }
8d063cd8 1385 ;