This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Remove code duplication in S_to_utf8_substr() and S_to_byte_substr()
[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.
166f8a29
DM
35 */
36
0de566d7 37/* Make the parser re-entrant. */
8d063cd8 38
0de566d7 39%pure_parser
8d063cd8 40
f05e27e5
DM
41/* FIXME for MAD - is the new mintro on while and until important? */
42
0de566d7 43%start prog
9d116dd7 44
8d063cd8 45%union {
d5c6462e
DM
46 I32 ival; /* __DEFAULT__ (marker for regen_perly.pl;
47 must always be 1st union member) */
79072805
LW
48 char *pval;
49 OP *opval;
50 GV *gvval;
2af555bf 51 AV* padval;
f05e27e5
DM
52#ifdef PERL_IN_MADLY_C
53 TOKEN* p_tkval;
123d08c9 54 TOKEN* i_tkval;
f05e27e5
DM
55#else
56 char *p_tkval;
123d08c9
DM
57 I32 i_tkval;
58#endif
59#ifdef PERL_MAD
60 TOKEN* tkval;
f05e27e5 61#endif
8d063cd8
LW
62}
63
123d08c9 64%token <i_tkval> '{' '}' '[' ']' '-' '+' '$' '@' '%' '*' '&' ';'
f0fcb552 65
a0d0e21e 66%token <opval> WORD METHOD FUNCMETH THING PMFUNC PRIVATEREF
4633a7c4 67%token <opval> FUNC0SUB UNIOPSUB LSTOPSUB
f05e27e5 68%token <p_tkval> LABEL
123d08c9
DM
69%token <i_tkval> FORMAT SUB ANONSUB PACKAGE USE
70%token <i_tkval> WHILE UNTIL IF UNLESS ELSE ELSIF CONTINUE FOR
71%token <i_tkval> GIVEN WHEN DEFAULT
72%token <i_tkval> LOOPEX DOTDOT
73%token <i_tkval> FUNC0 FUNC1 FUNC UNIOP LSTOP
74%token <i_tkval> RELOP EQOP MULOP ADDOP
75%token <i_tkval> DOLSHARP DO HASHBRACK NOAMP
76%token <i_tkval> LOCAL MY MYSUB REQUIRE
77%token <i_tkval> COLONATTR
f05e27e5
DM
78
79%type <ival> prog progstart remember mremember savescope
80%type <ival> startsub startanonsub startformsub
81/* FIXME for MAD - are these two ival? */
82%type <ival> mydefsv mintro
83
84%type <opval> decl format subrout mysubrout package use peg
85
bbce6d69 86%type <opval> block mblock lineseq line loop cond else
fad39ff1 87%type <opval> expr term subscripted scalar ary hsh arylen star amper sideff
a034e688 88%type <opval> argexpr nexpr texpr iexpr mexpr mnexpr miexpr
44a8e56a
PP
89%type <opval> listexpr listexprcom indirob listop method
90%type <opval> formname subname proto subbody cont my_scalar
f05e27e5 91%type <opval> subattrlist myattrlist myattrterm myterm
891be019 92%type <opval> termbinop termunop anonymous termdo
0d863452 93%type <opval> switch case
f05e27e5 94%type <p_tkval> label
2af555bf 95%type <padval> remempad
79072805 96
123d08c9 97%nonassoc <i_tkval> PREC_LOW
fad39ff1
SM
98%nonassoc LOOPEX
99
123d08c9
DM
100%left <i_tkval> OROP DOROP
101%left <i_tkval> ANDOP
102%right <i_tkval> NOTOP
36477c24 103%nonassoc LSTOP LSTOPSUB
123d08c9
DM
104%left <i_tkval> ','
105%right <i_tkval> ASSIGNOP
106%right <i_tkval> '?' ':'
8d063cd8 107%nonassoc DOTDOT
123d08c9
DM
108%left <i_tkval> OROR DORDOR
109%left <i_tkval> ANDAND
110%left <i_tkval> BITOROP
111%left <i_tkval> BITANDOP
a687059c
LW
112%nonassoc EQOP
113%nonassoc RELOP
36477c24 114%nonassoc UNIOP UNIOPSUB
20515881 115%nonassoc REQUIRE
123d08c9 116%left <i_tkval> SHIFTOP
a687059c
LW
117%left ADDOP
118%left MULOP
123d08c9
DM
119%left <i_tkval> MATCHOP
120%right <i_tkval> '!' '~' UMINUS REFGEN
121%right <i_tkval> POWOP
122%nonassoc <i_tkval> PREINC PREDEC POSTINC POSTDEC
123%left <i_tkval> ARROW
124%nonassoc <i_tkval> ')'
125%left <i_tkval> '('
fad39ff1 126%left '[' '{'
8d063cd8 127
123d08c9 128%token <i_tkval> PEG
7f46837f 129
8d063cd8
LW
130%% /* RULES */
131
891be019 132/* The whole program */
ecb2f335 133prog : progstart
ae986130 134 /*CONTINUED*/ lineseq
ecb2f335 135 { $$ = $1; newPROG(block_end($1,$2)); }
8d063cd8
LW
136 ;
137
891be019 138/* An ordinary block */
a687059c 139block : '{' remember lineseq '}'
f05e27e5
DM
140 { if (PL_copline > (line_t)IVAL($1))
141 PL_copline = (line_t)IVAL($1);
142 $$ = block_end($2, $3);
143 TOKEN_GETMAD($1,$$,'{');
144 TOKEN_GETMAD($4,$$,'}');
145 }
a0d0e21e
LW
146 ;
147
55497cff
PP
148remember: /* NULL */ /* start a full lexical scope */
149 { $$ = block_start(TRUE); }
150 ;
151
0d863452
RH
152mydefsv: /* NULL */ /* lexicalize $_ */
153 { $$ = (I32) allocmy("$_"); }
154 ;
155
ecb2f335
NIS
156progstart:
157 {
ecb2f335
NIS
158 PL_expect = XSTATE; $$ = block_start(TRUE);
159 }
160 ;
161
162
bbce6d69 163mblock : '{' mremember lineseq '}'
f05e27e5
DM
164 { if (PL_copline > (line_t)IVAL($1))
165 PL_copline = (line_t)IVAL($1);
166 $$ = block_end($2, $3);
167 TOKEN_GETMAD($1,$$,'{');
168 TOKEN_GETMAD($4,$$,'}');
169 }
55497cff
PP
170 ;
171
172mremember: /* NULL */ /* start a partial lexical scope */
173 { $$ = block_start(FALSE); }
8d063cd8
LW
174 ;
175
500bedb6
DM
176savescope: /* NULL */ /* remember stack pos in case of error */
177 { $$ = PL_savestack_ix; }
178
891be019 179/* A collection of "lines" in the program */
8d063cd8 180lineseq : /* NULL */
79072805
LW
181 { $$ = Nullop; }
182 | lineseq decl
f05e27e5
DM
183 {
184 $$ = IF_MAD(
185 append_list(OP_LINESEQ,
186 (LISTOP*)$1, (LISTOP*)$2),
187 $1);
188 }
500bedb6
DM
189 | lineseq savescope line
190 { LEAVE_SCOPE($2);
191 $$ = append_list(OP_LINESEQ,
192 (LISTOP*)$1, (LISTOP*)$3);
3280af22 193 PL_pad_reset_pending = TRUE;
500bedb6 194 if ($1 && $3) PL_hints |= HINT_BLOCK_SCOPE; }
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),
216 Nullop);
217 PL_copline = NOLINE;
218 TOKEN_FREE($1);
219 TOKEN_GETMAD($2,$$,';');
220 }
221 PL_expect = XSTATE;
222 }
8d063cd8 223 | label sideff ';'
f05e27e5
DM
224 {
225 $$ = newSTATEOP(0, PVAL($1), $2);
226 PL_expect = XSTATE;
227 DO_MAD(
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 }
237 )
238 }
8d063cd8
LW
239 ;
240
891be019 241/* An expression which may have a side-effect */
a687059c 242sideff : error
79072805 243 { $$ = Nullop; }
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
f05e27e5
DM
263 { $$ = newFOROP(0, Nullch, (line_t)IVAL($2),
264 Nullop, $3, $1, Nullop);
265 TOKEN_GETMAD($2,((LISTOP*)$$)->op_first->op_sibling,'w');
266 }
79072805
LW
267 ;
268
891be019 269/* else and elsif blocks */
79072805
LW
270else : /* NULL */
271 { $$ = Nullop; }
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
f05e27e5 277 { PL_copline = (line_t)IVAL($1);
2c15bef3 278 $$ = newCONDOP(0, $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
f05e27e5 288 { PL_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
f05e27e5 296 { PL_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
LW
314cont : /* NULL */
315 { $$ = Nullop; }
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
DM
324 { OP *innerop;
325 PL_copline = (line_t)$2;
36477c24 326 $$ = block_end($4,
f05e27e5
DM
327 newSTATEOP(0, PVAL($1),
328 innerop = newWHILEOP(0, 1, (LOOP*)Nullop,
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
DM
337 { OP *innerop;
338 PL_copline = (line_t)$2;
36477c24 339 $$ = block_end($4,
f05e27e5
DM
340 newSTATEOP(0, PVAL($1),
341 innerop = newWHILEOP(0, 1, (LOOP*)Nullop,
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),
373 Nullop, $5, $7, $8));
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;
f05e27e5
DM
383 PL_copline = (line_t)IVAL($2);
384 forop = newSTATEOP(0, PVAL($1),
36c66720 385 newWHILEOP(0, 1, (LOOP*)Nullop,
f05e27e5 386 IVAL($2), scalar($7),
a034e688 387 $12, $10, $9));
f05e27e5
DM
388#ifdef MAD
389 if (!$5)
390 $5 = newOP(OP_NULL, 0);
391 forop = newUNOP(OP_NULL, 0, append_elem(OP_LINESEQ,
392 newSTATEOP(0,
393 (($1)->tk_lval.pval
394 ?savepv(($1)->tk_lval.pval):Nullch),
395 $5),
396 forop));
397
398 token_getmad($2,forop,'3');
399 token_getmad($3,forop,'(');
400 token_getmad($6,forop,'1');
401 token_getmad($8,forop,'2');
402 token_getmad($11,forop,')');
403 token_getmad($1,forop,'L');
404#else
36c66720
RH
405 if ($5) {
406 forop = append_elem(OP_LINESEQ,
407 newSTATEOP(0, ($1?savepv($1):Nullch),
408 $5),
409 forop);
410 }
411
f05e27e5
DM
412
413#endif
36c66720 414 $$ = block_end($4, forop); }
79072805 415 | label block cont /* a block is a loop that happens once */
f05e27e5 416 { $$ = newSTATEOP(0, PVAL($1),
fb73857a 417 newWHILEOP(0, 1, (LOOP*)Nullop,
f05e27e5
DM
418 NOLINE, Nullop, $2, $3, 0));
419 TOKEN_GETMAD($1,((LISTOP*)$$)->op_first,'L'); }
8d063cd8
LW
420 ;
421
0d863452
RH
422/* Switch blocks */
423switch : label GIVEN '(' remember mydefsv mexpr ')' mblock
424 { PL_copline = (line_t) $2;
425 $$ = block_end($4,
f05e27e5 426 newSTATEOP(0, PVAL($1),
0d863452
RH
427 newGIVENOP($6, scope($8),
428 (PADOFFSET) $5) )); }
429 ;
430
a034e688
DM
431/* determine whether there are any new my declarations */
432mintro : /* NULL */
433 { $$ = (PL_min_intro_pending &&
434 PL_max_intro_pending >= PL_min_intro_pending);
435 intro_my(); }
436
891be019 437/* Normal expression */
8d063cd8 438nexpr : /* NULL */
79072805 439 { $$ = Nullop; }
8d063cd8
LW
440 | sideff
441 ;
442
891be019 443/* Boolean expression */
8d063cd8 444texpr : /* NULL means true */
f05e27e5
DM
445 { YYSTYPE tmplval;
446 (void)scan_num("1", &tmplval);
447 $$ = tmplval.opval; }
8d063cd8
LW
448 | expr
449 ;
450
891be019 451/* Inverted boolean expression */
55497cff
PP
452iexpr : expr
453 { $$ = invert(scalar($1)); }
454 ;
455
891be019 456/* Expression with its own lexical scope */
55497cff 457mexpr : expr
bbce6d69
PP
458 { $$ = $1; intro_my(); }
459 ;
460
461mnexpr : nexpr
462 { $$ = $1; intro_my(); }
55497cff
PP
463 ;
464
55497cff 465miexpr : iexpr
bbce6d69 466 { $$ = $1; intro_my(); }
55497cff
PP
467 ;
468
891be019 469/* Optional "MAIN:"-style loop labels */
8d063cd8 470label : /* empty */
f05e27e5
DM
471 {
472#ifdef MAD
473 YYSTYPE tmplval;
474 tmplval.pval = Nullch;
475 $$ = newTOKEN(OP_NULL, tmplval, 0);
476#else
477 $$ = Nullch;
478#endif
479 }
32c2e4fb 480 | LABEL
8d063cd8
LW
481 ;
482
f05e27e5 483/* Some kind of declaration - just hang on peg in the parse tree */
8d063cd8 484decl : format
f05e27e5 485 { $$ = $1; }
8d063cd8 486 | subrout
f05e27e5 487 { $$ = $1; }
09bef843 488 | mysubrout
f05e27e5 489 { $$ = $1; }
a687059c 490 | package
f05e27e5 491 { $$ = $1; }
a0d0e21e 492 | use
f05e27e5
DM
493 { $$ = $1; }
494
495 /* these two are only used by MAD */
496
497 | peg
498 { $$ = $1; }
499 ;
500
501peg : PEG
502 { $$ = newOP(OP_NULL,0);
503 TOKEN_GETMAD($1,$$,'p');
504 }
8d063cd8
LW
505 ;
506
2af555bf 507format : FORMAT remempad startformsub formname block
a8ff2fa6 508 { SvREFCNT_inc(PL_compcv);
f05e27e5 509#ifdef MAD
2af555bf 510 $$ = newFORM($3, $4, $5);
f05e27e5
DM
511 prepend_madprops($1->tk_mad, $$, 'F');
512 $1->tk_mad = 0;
513 token_free($1);
514#else
2af555bf 515 newFORM($3, $4, $5);
f05e27e5
DM
516#endif
517 }
44a8e56a
PP
518 ;
519
520formname: WORD { $$ = $1; }
521 | /* NULL */ { $$ = Nullop; }
8d063cd8
LW
522 ;
523
891be019 524/* Unimplemented "my sub foo { }" */
2af555bf 525mysubrout: MYSUB remempad startsub subname proto subattrlist subbody
a8ff2fa6 526 { SvREFCNT_inc(PL_compcv);
f05e27e5 527#ifdef MAD
2af555bf 528 $$ = newMYSUB($3, $4, $5, $6, $7);
f05e27e5
DM
529 token_getmad($1,$$,'d');
530#else
2af555bf 531 newMYSUB($3, $4, $5, $6, $7);
f05e27e5
DM
532#endif
533 }
09bef843
SB
534 ;
535
891be019 536/* Subroutine definition */
2af555bf 537subrout : SUB remempad startsub subname proto subattrlist subbody
a8ff2fa6 538 { SvREFCNT_inc(PL_compcv);
f05e27e5
DM
539#ifdef MAD
540 OP* o = newSVOP(OP_ANONCODE, 0,
2af555bf 541 (SV*)newATTRSUB($3, $4, $5, $6, $7));
f05e27e5
DM
542 $$ = newOP(OP_NULL,0);
543 op_getmad(o,$$,'&');
2af555bf
DM
544 op_getmad($4,$$,'n');
545 op_getmad($5,$$,'s');
546 op_getmad($6,$$,'a');
f05e27e5 547 token_getmad($1,$$,'d');
2af555bf
DM
548 append_madprops($7->op_madprop, $$, 0);
549 $7->op_madprop = 0;
f05e27e5 550#else
2af555bf 551 newATTRSUB($3, $4, $5, $6, $7);
f05e27e5
DM
552 $$ = Nullop;
553#endif
554 }
28757baa
PP
555 ;
556
2af555bf
DM
557remempad: /* NULL */ /* remember current value of PL_comppad */
558 { $$ = PL_comppad; }
559 ;
560
fa83b5b6 561startsub: /* NULL */ /* start a regular subroutine scope */
a8ff2fa6
DM
562 { $$ = start_subparse(FALSE, 0);
563 SAVEFREESV(PL_compcv); }
f05e27e5 564
28757baa
PP
565 ;
566
567startanonsub: /* NULL */ /* start an anonymous subroutine scope */
a8ff2fa6
DM
568 { $$ = start_subparse(FALSE, CVf_ANON);
569 SAVEFREESV(PL_compcv); }
28757baa
PP
570 ;
571
44a8e56a 572startformsub: /* NULL */ /* start a format subroutine scope */
a8ff2fa6
DM
573 { $$ = start_subparse(TRUE, 0);
574 SAVEFREESV(PL_compcv); }
44a8e56a
PP
575 ;
576
891be019 577/* Name of a subroutine - must be a bareword, could be special */
2596d9fe 578subname : WORD { const char *const name = SvPV_nolen_const(((SVOP*)$1)->op_sv);
e858de61 579 if (strEQ(name, "BEGIN") || strEQ(name, "END")
3c10abe3
AG
580 || strEQ(name, "INIT") || strEQ(name, "CHECK")
581 || strEQ(name, "UNITCHECK"))
1aff0e91 582 CvSPECIAL_on(PL_compcv);
28757baa 583 $$ = $1; }
a0d0e21e
LW
584 ;
585
891be019 586/* Subroutine prototype */
4633a7c4
LW
587proto : /* NULL */
588 { $$ = Nullop; }
589 | THING
590 ;
28757baa 591
891be019 592/* Optional list of subroutine attributes */
09bef843
SB
593subattrlist: /* NULL */
594 { $$ = Nullop; }
595 | COLONATTR THING
f05e27e5
DM
596 { $$ = $2;
597 TOKEN_GETMAD($1,$$,':');
598 }
09bef843 599 | COLONATTR
f05e27e5
DM
600 { $$ = IF_MAD(
601 newOP(OP_NULL, 0),
602 Nullop
603 );
604 TOKEN_GETMAD($1,$$,':');
605 }
09bef843
SB
606 ;
607
891be019 608/* List of attributes for a "my" variable declaration */
09bef843 609myattrlist: COLONATTR THING
f05e27e5
DM
610 { $$ = $2;
611 TOKEN_GETMAD($1,$$,':');
612 }
09bef843 613 | COLONATTR
f05e27e5
DM
614 { $$ = IF_MAD(
615 newOP(OP_NULL, 0),
616 Nullop
617 );
618 TOKEN_GETMAD($1,$$,':');
619 }
09bef843
SB
620 ;
621
891be019 622/* Subroutine body - either null or a block */
28757baa 623subbody : block { $$ = $1; }
f05e27e5
DM
624 | ';' { $$ = IF_MAD(
625 newOP(OP_NULL,0),
626 Nullop
627 );
628 PL_expect = XSTATE;
629 TOKEN_GETMAD($1,$$,';');
630 }
8d063cd8
LW
631 ;
632
a687059c 633package : PACKAGE WORD ';'
f05e27e5
DM
634 {
635#ifdef MAD
636 $$ = package($2);
637 token_getmad($1,$$,'o');
638 token_getmad($3,$$,';');
639#else
640 package($2);
641#endif
642 }
a687059c
LW
643 ;
644
2af555bf 645use : USE remempad startsub
1aff0e91 646 { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
28757baa 647 WORD WORD listexpr ';'
f05e27e5
DM
648 { SvREFCNT_inc(PL_compcv);
649#ifdef MAD
2af555bf 650 $$ = utilize(IVAL($1), $3, $5, $6, $7);
f05e27e5 651 token_getmad($1,$$,'o');
2af555bf 652 token_getmad($8,$$,';');
f05e27e5
DM
653 if (PL_rsfp_filters && AvFILLp(PL_rsfp_filters) >= 0)
654 append_madprops(newMADPROP('!', MAD_PV, "", 0), $$, 0);
655#else
2af555bf 656 utilize(IVAL($1), $3, $5, $6, $7);
f05e27e5
DM
657#endif
658 }
85e6fe83
LW
659 ;
660
891be019 661/* Ordinary expressions; logical combinations */
a0d0e21e 662expr : expr ANDOP expr
f05e27e5
DM
663 { $$ = newLOGOP(OP_AND, 0, $1, $3);
664 TOKEN_GETMAD($2,$$,'o');
665 }
a0d0e21e 666 | expr OROP expr
f05e27e5
DM
667 { $$ = newLOGOP(IVAL($2), 0, $1, $3);
668 TOKEN_GETMAD($2,$$,'o');
669 }
c963b151 670 | expr DOROP expr
f05e27e5
DM
671 { $$ = newLOGOP(OP_DOR, 0, $1, $3);
672 TOKEN_GETMAD($2,$$,'o');
673 }
fad39ff1 674 | argexpr %prec PREC_LOW
a0d0e21e
LW
675 ;
676
891be019 677/* Expressions are a list of terms joined by commas */
a0d0e21e 678argexpr : argexpr ','
f05e27e5
DM
679 {
680#ifdef MAD
681 OP* op = newNULLLIST();
682 token_getmad($2,op,',');
683 $$ = append_elem(OP_LIST, $1, op);
684#else
685 $$ = $1;
686#endif
687 }
a0d0e21e 688 | argexpr ',' term
f05e27e5
DM
689 {
690 DO_MAD(
691 $3 = newUNOP(OP_NULL, 0, $3);
692 token_getmad($2,$3,',');
693 )
694 $$ = append_elem(OP_LIST, $1, $3);
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 }
2af555bf 751 | LSTOPSUB remempad startanonsub block /* sub f(&@); f {foo} ... */
a8ff2fa6 752 { SvREFCNT_inc(PL_compcv);
2af555bf 753 $4 = newANONATTRSUB($3, 0, Nullop, $4); }
891be019 754 listexpr %prec LSTOP /* ... @bar */
4633a7c4 755 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
28757baa 756 append_elem(OP_LIST,
2af555bf 757 prepend_elem(OP_LIST, $4, $6), $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));
f05e27e5
DM
771 PL_expect = XOPERATOR;
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));
f05e27e5
DM
798 PL_expect = XOPERATOR;
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));
f05e27e5
DM
807 PL_expect = XOPERATOR;
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));
f05e27e5
DM
817 PL_expect = XOPERATOR;
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! */
f05e27e5
DM
859 { $$ = newSLICEOP(0, $4, Nullop);
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));
909 DO_MAD(
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');
916 )
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 | '[' ']'
f05e27e5
DM
989 { $$ = newANONLIST(Nullop);
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) */
f05e27e5
DM
1000 { $$ = newANONHASH(Nullop);
1001 TOKEN_GETMAD($1,$$,'{');
1002 TOKEN_GETMAD($2,$$,';');
1003 TOKEN_GETMAD($3,$$,'}');
1004 }
2af555bf 1005 | ANONSUB remempad startanonsub proto subattrlist block %prec '('
a8ff2fa6 1006 { SvREFCNT_inc(PL_compcv);
2af555bf 1007 $$ = newANONATTRSUB($3, $4, $5, $6);
f05e27e5 1008 TOKEN_GETMAD($1,$$,'o');
2af555bf
DM
1009 OP_GETMAD($4,$$,'s');
1010 OP_GETMAD($5,$$,'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)
f05e27e5
DM
1031 )),Nullop)); dep();
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,
f05e27e5
DM
1052 scalar(newCVREF(0,scalar($2))), Nullop)); dep();
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
DM
1085 { $$ = localize($2,IVAL($1));
1086 TOKEN_GETMAD($1,$$,'d');
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)));
f05e27e5
DM
1125 PL_expect = XOPERATOR;
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)));
1143 DO_MAD(
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,')');
1150 )
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 }
1182 | REQUIRE /* require, $_ implied *//* FIMXE for MAD needed? */
a72a1c8b 1183 { $$ = newOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0); }
f05e27e5 1184 | REQUIRE term /* require Foo *//* FIMXE for MAD needed? */
a72a1c8b 1185 { $$ = newUNOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0, $2); }
3cd0a11a
RGS
1186 | UNIOPSUB
1187 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
891be019 1188 | UNIOPSUB term /* Sub treated as unop */
4633a7c4
LW
1189 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1190 append_elem(OP_LIST, $2, scalar($1))); }
891be019 1191 | FUNC0 /* Nullary operator */
f05e27e5
DM
1192 { $$ = newOP(IVAL($1), 0);
1193 TOKEN_GETMAD($1,$$,'o');
1194 }
ae986130 1195 | FUNC0 '(' ')'
f05e27e5
DM
1196 { $$ = newOP(IVAL($1), 0);
1197 TOKEN_GETMAD($1,$$,'o');
1198 TOKEN_GETMAD($2,$$,'(');
1199 TOKEN_GETMAD($3,$$,')');
1200 }
891be019 1201 | FUNC0SUB /* Sub treated as nullop */
28757baa 1202 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
4633a7c4 1203 scalar($1)); }
891be019 1204 | FUNC1 '(' ')' /* not () */
f05e27e5
DM
1205 { $$ = (IVAL($1) == OP_NOT)
1206 ? newUNOP(IVAL($1), 0, newSVOP(OP_CONST, 0, newSViv(0)))
1207 : newOP(IVAL($1), OPf_SPECIAL);
1208
1209 TOKEN_GETMAD($1,$$,'o');
1210 TOKEN_GETMAD($2,$$,'(');
1211 TOKEN_GETMAD($3,$$,')');
1212 }
891be019 1213 | FUNC1 '(' expr ')' /* not($foo) */
f05e27e5
DM
1214 { $$ = newUNOP(IVAL($1), 0, $3);
1215 TOKEN_GETMAD($1,$$,'o');
1216 TOKEN_GETMAD($2,$$,'(');
1217 TOKEN_GETMAD($4,$$,')');
1218 }
1219 | PMFUNC '(' argexpr ')' /* m//, s///, tr/// */
1220 { $$ = pmruntime($1, $3, 1);
1221 TOKEN_GETMAD($2,$$,'(');
1222 TOKEN_GETMAD($4,$$,')');
1223 }
79072805 1224 | WORD
378cc40b 1225 | listop
8d063cd8
LW
1226 ;
1227
891be019 1228/* "my" declarations, with optional attributes */
09bef843 1229myattrterm: MY myterm myattrlist
f05e27e5
DM
1230 { $$ = my_attrs($2,$3);
1231 DO_MAD(
1232 token_getmad($1,$$,'d');
1233 append_madprops($3->op_madprop, $$, 'a');
1234 $3->op_madprop = 0;
1235 )
1236 }
09bef843 1237 | MY myterm
f05e27e5
DM
1238 { $$ = localize($2,IVAL($1));
1239 TOKEN_GETMAD($1,$$,'d');
1240 }
09bef843
SB
1241 ;
1242
891be019 1243/* Things that can be "my"'d */
09bef843 1244myterm : '(' expr ')'
f05e27e5
DM
1245 { $$ = sawparens($2);
1246 TOKEN_GETMAD($1,$$,'(');
1247 TOKEN_GETMAD($3,$$,')');
1248 }
09bef843 1249 | '(' ')'
f05e27e5
DM
1250 { $$ = sawparens(newNULLLIST());
1251 TOKEN_GETMAD($1,$$,'(');
1252 TOKEN_GETMAD($2,$$,')');
1253 }
09bef843
SB
1254 | scalar %prec '('
1255 { $$ = $1; }
1256 | hsh %prec '('
1257 { $$ = $1; }
1258 | ary %prec '('
1259 { $$ = $1; }
1260 ;
1261
891be019 1262/* Basic list expressions */
fad39ff1 1263listexpr: /* NULL */ %prec PREC_LOW
8990e307 1264 { $$ = Nullop; }
fad39ff1 1265 | argexpr %prec PREC_LOW
a0d0e21e
LW
1266 { $$ = $1; }
1267 ;
1268
1269listexprcom: /* NULL */
1270 { $$ = Nullop; }
79072805
LW
1271 | expr
1272 { $$ = $1; }
a0d0e21e 1273 | expr ','
f05e27e5
DM
1274 {
1275#ifdef MAD
1276 OP* op = newNULLLIST();
1277 token_getmad($2,op,',');
1278 $$ = append_elem(OP_LIST, $1, op);
1279#else
1280 $$ = $1;
1281#endif
1282
1283 }
79072805
LW
1284 ;
1285
891be019
SC
1286/* A little bit of trickery to make "for my $foo (@bar)" actually be
1287 lexical */
55497cff 1288my_scalar: scalar
3280af22 1289 { PL_in_my = 0; $$ = my($1); }
55497cff
PP
1290 ;
1291
79072805 1292amper : '&' indirob
f05e27e5
DM
1293 { $$ = newCVREF(IVAL($1),$2);
1294 TOKEN_GETMAD($1,$$,'&');
1295 }
a687059c
LW
1296 ;
1297
79072805 1298scalar : '$' indirob
f05e27e5
DM
1299 { $$ = newSVREF($2);
1300 TOKEN_GETMAD($1,$$,'$');
1301 }
a687059c
LW
1302 ;
1303
79072805 1304ary : '@' indirob
f05e27e5
DM
1305 { $$ = newAVREF($2);
1306 TOKEN_GETMAD($1,$$,'@');
1307 }
79072805
LW
1308 ;
1309
1310hsh : '%' indirob
f05e27e5
DM
1311 { $$ = newHVREF($2);
1312 TOKEN_GETMAD($1,$$,'%');
1313 }
79072805
LW
1314 ;
1315
1316arylen : DOLSHARP indirob
f05e27e5
DM
1317 { $$ = newAVREF($2);
1318 TOKEN_GETMAD($1,$$,'l');
1319 }
79072805
LW
1320 ;
1321
1322star : '*' indirob
f05e27e5
DM
1323 { $$ = newGVREF(0,$2);
1324 TOKEN_GETMAD($1,$$,'*');
1325 }
79072805
LW
1326 ;
1327
891be019 1328/* Indirect objects */
79072805
LW
1329indirob : WORD
1330 { $$ = scalar($1); }
fad39ff1 1331 | scalar %prec PREC_LOW
f05e27e5 1332 { $$ = scalar($1); }
79072805 1333 | block
a0d0e21e 1334 { $$ = scope($1); }
79072805 1335
93a17b20
LW
1336 | PRIVATEREF
1337 { $$ = $1; }
8d063cd8 1338 ;