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