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