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