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