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