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