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