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