This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[perl #78716] Bogus read after seek beyond end of string
[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
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
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 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 141/* Top-level choice of what kind of thing yyparse was called to parse */
727a8fe5
Z
142grammar : GRAMPROG
143 {
144 PL_parser->expect = XSTATE;
145 }
146 remember stmtseq
147 {
148 newPROG(block_end($3,$4));
149 $$ = 0;
150 }
e53d8f76
Z
151 | GRAMBLOCK
152 {
153 parser->expect = XBLOCK;
154 }
155 block
156 {
5f211341 157 PL_pad_reset_pending = TRUE;
8359b381
Z
158 PL_eval_root = $3;
159 $$ = 0;
160 yyunlex();
161 parser->yychar = YYEOF;
162 }
163 | GRAMBARESTMT
164 {
165 parser->expect = XSTATE;
166 }
167 barestmt
168 {
169 PL_pad_reset_pending = TRUE;
e53d8f76
Z
170 PL_eval_root = $3;
171 $$ = 0;
172 yyunlex();
173 parser->yychar = YYEOF;
174 }
9eb5c532 175 | GRAMFULLSTMT
28ac2b49 176 {
9eb5c532
Z
177 parser->expect = XSTATE;
178 }
179 fullstmt
180 {
5f211341 181 PL_pad_reset_pending = TRUE;
9eb5c532 182 PL_eval_root = $3;
28ac2b49
Z
183 $$ = 0;
184 yyunlex();
185 parser->yychar = YYEOF;
186 }
07ffcb73
Z
187 | GRAMSTMTSEQ
188 {
189 parser->expect = XSTATE;
190 }
5f211341 191 stmtseq
07ffcb73
Z
192 {
193 PL_eval_root = $3;
194 $$ = 0;
195 }
196 ;
197
891be019 198/* An ordinary block */
5f211341 199block : '{' remember stmtseq '}'
53a7735b
DM
200 { if (PL_parser->copline > (line_t)IVAL($1))
201 PL_parser->copline = (line_t)IVAL($1);
f05e27e5
DM
202 $$ = block_end($2, $3);
203 TOKEN_GETMAD($1,$$,'{');
204 TOKEN_GETMAD($4,$$,'}');
205 }
a0d0e21e
LW
206 ;
207
55497cff 208remember: /* NULL */ /* start a full lexical scope */
209 { $$ = block_start(TRUE); }
210 ;
211
0d863452 212mydefsv: /* NULL */ /* lexicalize $_ */
d6447115 213 { $$ = (I32) Perl_allocmy(aTHX_ STR_WITH_LEN("$_"), 0); }
0d863452
RH
214 ;
215
5f211341 216mblock : '{' mremember stmtseq '}'
53a7735b
DM
217 { if (PL_parser->copline > (line_t)IVAL($1))
218 PL_parser->copline = (line_t)IVAL($1);
f05e27e5
DM
219 $$ = block_end($2, $3);
220 TOKEN_GETMAD($1,$$,'{');
221 TOKEN_GETMAD($4,$$,'}');
222 }
55497cff 223 ;
224
225mremember: /* NULL */ /* start a partial lexical scope */
226 { $$ = block_start(FALSE); }
8d063cd8
LW
227 ;
228
eae48c89 229/* A sequence of statements in the program */
5f211341 230stmtseq : /* NULL */
bcabcc50 231 { $$ = (OP*)NULL; }
5f211341 232 | stmtseq fullstmt
2fcb4757 233 { $$ = op_append_list(OP_LINESEQ, $1, $2);
3280af22 234 PL_pad_reset_pending = TRUE;
503de470
DM
235 if ($1 && $2)
236 PL_hints |= HINT_BLOCK_SCOPE;
237 }
8d063cd8
LW
238 ;
239
eae48c89
Z
240/* A statement in the program, including optional label */
241fullstmt: label barestmt
242 {
243 if (PVAL($1) || $2) {
244 $$ = newSTATEOP(0, PVAL($1), $2);
245 TOKEN_GETMAD($1,
246 $2 ? cLISTOPx($$)->op_first : $$, 'L');
247 } else {
248 $$ = IF_MAD(newOP(OP_NULL, 0), $2);
249 }
250 }
251 ;
252
253/* A bare statement, lacking label and other aspects of state op */
254barestmt: PLUGSTMT
0d863452 255 { $$ = $1; }
eae48c89
Z
256 | PEG
257 {
258 $$ = newOP(OP_NULL,0);
259 TOKEN_GETMAD($1,$$,'p');
260 }
261 | FORMAT startformsub formname block
f05e27e5 262 {
eae48c89
Z
263 CV *fmtcv = PL_compcv;
264 SvREFCNT_inc_simple_void(PL_compcv);
265#ifdef MAD
266 $$ = newFORM($2, $3, $4);
267 prepend_madprops($1->tk_mad, $$, 'F');
268 $1->tk_mad = 0;
269 token_free($1);
270#else
271 newFORM($2, $3, $4);
272 $$ = (OP*)NULL;
273#endif
274 if (CvOUTSIDE(fmtcv) && !CvUNIQUE(CvOUTSIDE(fmtcv))) {
275 SvREFCNT_inc_simple_void(fmtcv);
276 pad_add_anon((SV*)fmtcv, OP_NULL);
f05e27e5 277 }
eae48c89
Z
278 }
279 | SUB startsub subname proto subattrlist subbody
280 {
281 SvREFCNT_inc_simple_void(PL_compcv);
282#ifdef MAD
283 {
284 OP* o = newSVOP(OP_ANONCODE, 0,
285 (SV*)newATTRSUB($2, $3, $4, $5, $6));
286 $$ = newOP(OP_NULL,0);
287 op_getmad(o,$$,'&');
288 op_getmad($3,$$,'n');
289 op_getmad($4,$$,'s');
290 op_getmad($5,$$,'a');
291 token_getmad($1,$$,'d');
292 append_madprops($6->op_madprop, $$, 0);
293 $6->op_madprop = 0;
f05e27e5 294 }
eae48c89
Z
295#else
296 newATTRSUB($2, $3, $4, $5, $6);
297 $$ = (OP*)NULL;
298#endif
f05e27e5 299 }
eae48c89 300 | MYSUB startsub subname proto subattrlist subbody
f05e27e5 301 {
eae48c89
Z
302 /* Unimplemented "my sub foo { }" */
303 SvREFCNT_inc_simple_void(PL_compcv);
304#ifdef MAD
305 $$ = newMYSUB($2, $3, $4, $5, $6);
306 token_getmad($1,$$,'d');
307#else
308 newMYSUB($2, $3, $4, $5, $6);
309 $$ = (OP*)NULL;
310#endif
311 }
312 | PACKAGE WORD WORD ';'
5f211341 313 {
eae48c89
Z
314#ifdef MAD
315 $$ = package($3);
316 token_getmad($1,$$,'o');
317 if ($2)
318 package_version($2);
319 token_getmad($4,$$,';');
320#else
321 package($3);
322 if ($2)
323 package_version($2);
324 $$ = (OP*)NULL;
325#endif
326 }
327 | USE startsub
328 { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
329 WORD WORD listexpr ';'
330 {
331 SvREFCNT_inc_simple_void(PL_compcv);
332#ifdef MAD
333 $$ = utilize(IVAL($1), $2, $4, $5, $6);
334 token_getmad($1,$$,'o');
335 token_getmad($7,$$,';');
336 if (PL_parser->rsfp_filters &&
337 AvFILLp(PL_parser->rsfp_filters) >= 0)
338 append_madprops(newMADPROP('!', MAD_NULL, NULL, 0), $$, 0);
339#else
340 utilize(IVAL($1), $2, $4, $5, $6);
341 $$ = (OP*)NULL;
342#endif
343 }
344 | IF lpar_or_qw remember mexpr ')' mblock else
345 {
3ad73efd
Z
346 $$ = block_end($3,
347 newCONDOP(0, $4, op_scope($6), $7));
eae48c89
Z
348 TOKEN_GETMAD($1,$$,'I');
349 TOKEN_GETMAD($2,$$,'(');
350 TOKEN_GETMAD($5,$$,')');
351 PL_parser->copline = (line_t)IVAL($1);
352 }
353 | UNLESS lpar_or_qw remember miexpr ')' mblock else
354 {
3ad73efd
Z
355 $$ = block_end($3,
356 newCONDOP(0, $4, op_scope($6), $7));
eae48c89
Z
357 TOKEN_GETMAD($1,$$,'I');
358 TOKEN_GETMAD($2,$$,'(');
359 TOKEN_GETMAD($5,$$,')');
360 PL_parser->copline = (line_t)IVAL($1);
361 }
362 | GIVEN lpar_or_qw remember mydefsv mexpr ')' mblock
363 {
364 $$ = block_end($3,
3ad73efd 365 newGIVENOP($5, op_scope($7), (PADOFFSET)$4));
eae48c89
Z
366 PL_parser->copline = (line_t)IVAL($1);
367 }
368 | WHEN lpar_or_qw remember mexpr ')' mblock
3ad73efd 369 { $$ = block_end($3, newWHENOP($4, op_scope($6))); }
eae48c89 370 | DEFAULT block
3ad73efd 371 { $$ = newWHENOP(0, op_scope($2)); }
eae48c89
Z
372 | WHILE lpar_or_qw remember texpr ')' mintro mblock cont
373 {
374 $$ = block_end($3,
375 newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
94bf0465 376 $4, $7, $8, $6));
eae48c89
Z
377 TOKEN_GETMAD($1,$$,'W');
378 TOKEN_GETMAD($2,$$,'(');
379 TOKEN_GETMAD($5,$$,')');
380 PL_parser->copline = (line_t)IVAL($1);
381 }
382 | UNTIL lpar_or_qw remember iexpr ')' mintro mblock cont
383 {
384 $$ = block_end($3,
94bf0465
Z
385 newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
386 $4, $7, $8, $6));
eae48c89
Z
387 TOKEN_GETMAD($1,$$,'W');
388 TOKEN_GETMAD($2,$$,'(');
389 TOKEN_GETMAD($5,$$,')');
390 PL_parser->copline = (line_t)IVAL($1);
391 }
392 | FOR lpar_or_qw remember mnexpr ';' texpr ';' mintro mnexpr ')'
393 mblock
394 {
395 OP *initop = IF_MAD($4 ? $4 : newOP(OP_NULL, 0), $4);
396 OP *forop = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
94bf0465 397 scalar($6), $11, $9, $8);
eae48c89
Z
398 if (initop) {
399 forop = op_prepend_elem(OP_LINESEQ, initop,
400 op_append_elem(OP_LINESEQ,
401 newOP(OP_UNSTACK, OPf_SPECIAL),
402 forop));
5f211341 403 }
eae48c89
Z
404 DO_MAD({ forop = newUNOP(OP_NULL, 0, forop); })
405 $$ = block_end($3, forop);
406 TOKEN_GETMAD($1,$$,'3');
407 TOKEN_GETMAD($2,$$,'(');
408 TOKEN_GETMAD($5,$$,'1');
409 TOKEN_GETMAD($7,$$,'2');
410 TOKEN_GETMAD($10,$$,')');
411 PL_parser->copline = (line_t)IVAL($1);
412 }
413 | FOR MY remember my_scalar lpar_or_qw mexpr ')' mblock cont
414 {
94bf0465 415 $$ = block_end($3, newFOROP(0, $4, $6, $8, $9));
eae48c89
Z
416 TOKEN_GETMAD($1,$$,'W');
417 TOKEN_GETMAD($2,$$,'d');
418 TOKEN_GETMAD($5,$$,'(');
419 TOKEN_GETMAD($7,$$,')');
420 PL_parser->copline = (line_t)IVAL($1);
421 }
422 | FOR scalar lpar_or_qw remember mexpr ')' mblock cont
423 {
94bf0465 424 $$ = block_end($4, newFOROP(0,
3ad73efd 425 op_lvalue($2, OP_ENTERLOOP), $5, $7, $8));
eae48c89
Z
426 TOKEN_GETMAD($1,$$,'W');
427 TOKEN_GETMAD($3,$$,'(');
428 TOKEN_GETMAD($6,$$,')');
429 PL_parser->copline = (line_t)IVAL($1);
430 }
431 | FOR lpar_or_qw remember mexpr ')' mblock cont
432 {
433 $$ = block_end($3,
94bf0465 434 newFOROP(0, (OP*)NULL, $4, $6, $7));
eae48c89
Z
435 TOKEN_GETMAD($1,$$,'W');
436 TOKEN_GETMAD($2,$$,'(');
437 TOKEN_GETMAD($5,$$,')');
438 PL_parser->copline = (line_t)IVAL($1);
439 }
440 | block cont
441 {
442 /* a block is a loop that happens once */
443 $$ = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
94bf0465 444 (OP*)NULL, $1, $2, 0);
eae48c89
Z
445 }
446 | PACKAGE WORD WORD '{' remember
447 {
448 int save_3_latefree = $3->op_latefree;
449 $3->op_latefree = 1;
450 package($3);
451 $3->op_latefree = save_3_latefree;
452 if ($2) {
453 int save_2_latefree = $2->op_latefree;
454 $2->op_latefree = 1;
455 package_version($2);
456 $2->op_latefree = save_2_latefree;
457 }
458 }
459 stmtseq '}'
460 {
461 /* a block is a loop that happens once */
94bf0465 462 $$ = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
eae48c89
Z
463 (OP*)NULL, block_end($5, $7), (OP*)NULL, 0);
464 op_free($3);
465 if ($2)
466 op_free($2);
467 TOKEN_GETMAD($4,$$,'{');
468 TOKEN_GETMAD($8,$$,'}');
469 if (PL_parser->copline > (line_t)IVAL($4))
470 PL_parser->copline = (line_t)IVAL($4);
471 }
472 | sideff ';'
473 {
474 PL_parser->expect = XSTATE;
475 $$ = $1;
476 TOKEN_GETMAD($2,$$,';');
477 }
478 | ';'
479 {
480 PL_parser->expect = XSTATE;
481 $$ = IF_MAD(newOP(OP_NULL, 0), (OP*)NULL);
482 TOKEN_GETMAD($1,$$,';');
483 PL_parser->copline = NOLINE;
5f211341 484 }
8d063cd8
LW
485 ;
486
891be019 487/* An expression which may have a side-effect */
a687059c 488sideff : error
bcabcc50 489 { $$ = (OP*)NULL; }
a687059c 490 | expr
79072805 491 { $$ = $1; }
a687059c 492 | expr IF expr
f05e27e5
DM
493 { $$ = newLOGOP(OP_AND, 0, $3, $1);
494 TOKEN_GETMAD($2,$$,'i');
495 }
a687059c 496 | expr UNLESS expr
f05e27e5
DM
497 { $$ = newLOGOP(OP_OR, 0, $3, $1);
498 TOKEN_GETMAD($2,$$,'i');
499 }
a687059c 500 | expr WHILE expr
f05e27e5
DM
501 { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1);
502 TOKEN_GETMAD($2,$$,'w');
503 }
55497cff 504 | expr UNTIL iexpr
f05e27e5
DM
505 { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1);
506 TOKEN_GETMAD($2,$$,'w');
507 }
ecca16b0 508 | expr FOR expr
94bf0465 509 { $$ = newFOROP(0, (OP*)NULL, $3, $1, (OP*)NULL);
eae48c89 510 TOKEN_GETMAD($2,$$,'w');
94bf0465 511 PL_parser->copline = (line_t)IVAL($2);
f05e27e5 512 }
f20dcd76 513 | expr WHEN expr
3ad73efd 514 { $$ = newWHENOP($3, op_scope($1)); }
79072805
LW
515 ;
516
891be019 517/* else and elsif blocks */
79072805 518else : /* NULL */
bcabcc50 519 { $$ = (OP*)NULL; }
55497cff 520 | ELSE mblock
3ad73efd
Z
521 {
522 ($2)->op_flags |= OPf_PARENS;
523 $$ = op_scope($2);
f05e27e5
DM
524 TOKEN_GETMAD($1,$$,'o');
525 }
ea25a9b2 526 | ELSIF lpar_or_qw mexpr ')' mblock else
53a7735b 527 { PL_parser->copline = (line_t)IVAL($1);
3ad73efd
Z
528 $$ = newCONDOP(0,
529 newSTATEOP(OPf_SPECIAL,NULL,$3),
530 op_scope($5), $6);
531 PL_hints |= HINT_BLOCK_SCOPE;
f05e27e5
DM
532 TOKEN_GETMAD($1,$$,'I');
533 TOKEN_GETMAD($2,$$,'(');
534 TOKEN_GETMAD($4,$$,')');
535 }
79072805
LW
536 ;
537
891be019 538/* Continue blocks */
79072805 539cont : /* NULL */
bcabcc50 540 { $$ = (OP*)NULL; }
79072805 541 | CONTINUE block
3ad73efd
Z
542 {
543 $$ = op_scope($2);
f05e27e5
DM
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 569iexpr : expr
570 { $$ = invert(scalar($1)); }
571 ;
572
891be019 573/* Expression with its own lexical scope */
55497cff 574mexpr : expr
bbce6d69 575 { $$ = $1; intro_my(); }
576 ;
577
578mnexpr : nexpr
579 { $$ = $1; intro_my(); }
55497cff 580 ;
581
55497cff 582miexpr : iexpr
bbce6d69 583 { $$ = $1; intro_my(); }
55497cff 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 608 ;
609
610startanonsub: /* NULL */ /* start an anonymous subroutine scope */
a8ff2fa6
DM
611 { $$ = start_subparse(FALSE, CVf_ANON);
612 SAVEFREESV(PL_compcv); }
28757baa 613 ;
614
44a8e56a 615startformsub: /* NULL */ /* start a format subroutine scope */
a8ff2fa6
DM
616 { $$ = start_subparse(TRUE, 0);
617 SAVEFREESV(PL_compcv); }
44a8e56a 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,
3ad73efd 982 op_lvalue(scalar($1), OP_POSTINC));
f05e27e5
DM
983 TOKEN_GETMAD($2,$$,'o');
984 }
891be019 985 | term POSTDEC /* $x-- */
79072805 986 { $$ = newUNOP(OP_POSTDEC, 0,
3ad73efd 987 op_lvalue(scalar($1), OP_POSTDEC));
f05e27e5
DM
988 TOKEN_GETMAD($2,$$,'o');
989 }
891be019 990 | PREINC term /* ++$x */
79072805 991 { $$ = newUNOP(OP_PREINC, 0,
3ad73efd 992 op_lvalue(scalar($2), OP_PREINC));
f05e27e5
DM
993 TOKEN_GETMAD($1,$$,'o');
994 }
891be019 995 | PREDEC term /* --$x */
79072805 996 { $$ = newUNOP(OP_PREDEC, 0,
3ad73efd 997 op_lvalue(scalar($2), OP_PREDEC));
f05e27e5
DM
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 */
3ad73efd 1042 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, op_scope($2));
f05e27e5
DM
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 */
3ad73efd 1100 { $$ = newUNOP(OP_REFGEN, 0, op_lvalue($2,OP_REFGEN));
f05e27e5
DM
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 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
3ad73efd 1365 { $$ = op_scope($1); }
79072805 1366
93a17b20
LW
1367 | PRIVATEREF
1368 { $$ = $1; }
8d063cd8 1369 ;