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