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