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