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